Jump to content
Yaerox

Capture DOS/PuTTY output - StdoutRead just working the first time

Recommended Posts

Yaerox
Hey guys,
 
I want to use Putty/Plink to connect on a remote computer via SSH because I need to execute a couple commands there. So I researched a couple days now and tried like 20 different ways to get this running but I'm still failing. Maybe a little description what my script should do:
 
#Edit: Short version: I need to use StdoutRead to get the DOS command promt output MORE THAN ONCE! The first time is working, but I need to get this the whole time till I say stop ^^
 
I want to start Putty/Plink and connect via SSH on a remote computer AND I wanna check if there is anything wrong going on or not, because the next step would be the execution of an perl-script on the remote computer which is giving an output text while running. That means I'd like to be able to read out the Putty-Window/Plink-Console the whole time ... I'm actual just getting the text-output after connecting.
 
Maybe a little example of what I'm expecting in some kind of mixed "AutoIt/Pseudo -Code":
 
#include <Constants.au3>
Local $iPID = Run('"C:\Program Files (x86)\Putty\putty.exe" -ssh root@HOST:22 -pw PASSWORD', @ScriptDir, @SW_SHOW, $STDERR_CHILD + $STDOUT_CHILD)
 
Actual Putty-Window is popping up showing me:
Using username "root".
Linux COMPUTERNAME 2.6.31-22-generic-pae #63-Ubuntu SMP Wed Aug 18 23:57:18 UTC 2010 i686
 
To access official Ubuntu documentation, please visit:
http://help.ubuntu.com/
 
  System information as of Mo 13. Jan 15:01:50 CET 2014
 
  System load:  0.0               Processes:           77
  Usage of /:   47.5% of 9.15GB   Users logged in:     1
  Memory usage: 9%                IP address for eth2: HOST
  Swap usage:   0%
 
  Graph this data and manage this system at https://landscape.canonical.com/
 
Last login: Mon Jan 13 14:58:43 2014 from XXX.X.X.XXX
root@COMPUTERNAME:~#
 
Now I'd like to do
ControlSend($hWindow_Putty, '', '', 'perl -w /some/dir/script.pl{ENTER}')
 
If I'd do this on my on with Putty I'd now see something like:
 
Using username "root".
Linux COMPUTERNAME 2.6.31-22-generic-pae #63-Ubuntu SMP Wed Aug 18 23:57:18 UTC 2010 i686
 
To access official Ubuntu documentation, please visit:
http://help.ubuntu.com/
 
  System information as of Mo 13. Jan 15:01:50 CET 2014
 
  System load:  0.0               Processes:           77
  Usage of /:   47.5% of 9.15GB   Users logged in:     1
  Memory usage: 9%                IP address for eth2: HOST
  Swap usage:   0%
 
  Graph this data and manage this system at https://landscape.canonical.com/
 
Last login: Mon Jan 13 14:58:43 2014 from XXX.X.X.XXX
root@COMPUTERNAME:~# perl -w some/dir/script.pl
some text written by the script.pl
some text written by the script.pl
some text written by the script.pl
root@COMPUTERNAME:~#
 
And that's what my problem is. I need to be able to check this output of the script like "some text written by the script.pl" because I may not execute a following statement if this one didn't succeed. In my thoughts the first step should show me what I got in the first code-box above, and the second output should be
perl -w some/dir/script.pl
some text written by the script.pl
some text written by the script.pl
some text written by the script.pl
root@COMPUTERNAME:~#
but I dont get this working ...
 
So I found many threads about Plink. I tried the whole thing with Plink...
 
Local $sData = ''
Local $iPID = Run('"C:\Program Files (x86)\Putty\plink.exe" -ssh root@HOST:22 -pw PASSWORD', @ScriptDir, @SW_SHOW, $STDERR_CHILD + $STDOUT_CHILD)
 
While True
    $sData &= StdoutRead($iPID)
    If @error Then ExitLoop
    Sleep(25)
WEnd
 
ConsoleWrite("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" & @CR & _
$sData & @CR & _
"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" & @CR)
 I'm getting the following output:
Linux COMPUTERNAME 2.6.31-22-generic-pae #63-Ubuntu SMP Wed Aug 18 23:57:18 UTC 2010 i686
 
To access official Ubuntu documentation, please visit:
http://help.ubuntu.com/
 
  System information as of Mo 13. Jan 15:35:53 CET 2014
 
  System load:  0.0               Processes:           77
  Usage of /:   47.5% of 9.15GB   Users logged in:     1
  Memory usage: 9%                IP address for eth2: HOST
  Swap usage:   0%
 
  Graph this data and manage this system at https://landscape.canonical.com/
 
Last login: Mon Jan 13 15:23:36 2014 from MY_IP_ADRESS
 
In comparison to the Putty window-output I'm missing here the 
root@COMPUTERNAME:~#
line ... anyway, I don't get this Plink thing doing more then just connecting ... no more executions I'm getting done ...
 
I also tried something like:
#RequireAdmin
#include <Constants.au3>
 
Local $sData = ''
Local $iPID = ShellExecute(@ComSpec, '"C:\Program Files (x86)\Putty\plink.exe" -ssh root@HOST:22 -pw PASSWORD', @ScriptDir, '', @SW_SHOW)
 
Local $hWindow = WinWait('Administrator: C:\Windows\system32\cmd.exe', '', 5)
ConsoleWrite("WinWait: " & $hWindow & @CR)
If $hWindow = 0 Then Exit
ConsoleWrite("WinActivate: " & WinActivate('Administrator: C:\Windows\system32\cmd.exe') & @CR)
ConsoleWrite("WinActive: " & WinActive('Administrator: C:\Windows\system32\cmd.exe') & @CR)
 
Sleep(1000)
ConsoleWrite(ControlSend('Administrator: C:\Windows\system32\cmd.exe', '', '', '"C:\Program Files (x86)\Putty\plink.exe" -ssh root@HOST:22 -pw PASSWORD{ENTER}') & @CR)
 
;StdinWrite($iPID, '"C:\Program Files (x86)\Putty\plink.exe" -ssh root@HOST:22 -pw PASSWORD' & @CRLF)
;StdinWrite($iPID)
 
While True
    $sData &= StdoutRead($iPID)
    If @error Then ExitLoop
    Sleep(25)
WEnd
 
ConsoleWrite("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" & @CR & _
$sData & @CR & _
"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" & @CR)
but unfortunately I dont get anything out of it ...
 
Maybe a last more example ... I found this on Stackoverflow (http://stackoverflow.com/questions/19206834/command-prompt-and-autoit-stdinwrite)
Thats my code after reading that article:
#include <Constants.au3>
 
Local $data
Local $pid = Run("C:\Windows\system32\cmd.exe",@SystemDir, @SW_HIDE, $STDIN_CHILD + $STDOUT_CHILD)
StdinWrite($pid,"cd C:\users\username")
StdinWrite($pid,@CRLF)
;~ StdinWrite($pid)
 
Sleep(2000)
 
$data &= StdoutRead($pid)
ConsoleWrite("Debug:" & $data & @LF)
 
$data = ''
 
StdinWrite($pid,"cd C:\Program Files (x86)")
StdinWrite($pid,@CRLF)
StdinWrite($pid)
 
$data &= StdoutRead($pid)
ConsoleWrite("Debug:" & $data & @LF)
but the second output still empty...
 
Can you guys please help me out?
Thanks in advice.
Regards.
Edited by MikeWenzel

Share this post


Link to post
Share on other sites
wakillon

May be like this ?

#Include <Constants.au3>

$sData = ''
$sStderrRead=''
$sStdoutRead=''
$iPid = Run ( '"' & @ProgramFilesDir & '\Putty\plink.exe" -ssh root@HOST:22 -pw PASSWORD', '', @SW_SHOW, $STDERR_CHILD + $STDOUT_CHILD)

While ProcessExists ( $iPid )
    $sStderrRead = StderrRead ( $iPid )
    If Not @error And $sStderrRead <> '' Then
;~      ConsoleWrite ( "! STDERR read : " & $sStderrRead & @Crlf )
    EndIf
    $sStdoutRead = StdoutRead ( $iPid )
    If Not @error And $sStdoutRead <> '' Then
        $sData &= $sStdoutRead
;~      ConsoleWrite ( "+ STDOUT read : " & $sStdoutRead & @Crlf )
    EndIf
;~   Sleep(25)
Wend

ConsoleWrite("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" & @CR & _
$sData & @CR & _
"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" & @CR)

AutoIt 3.3.14.2 X86 - SciTE 3.6.0WIN 8.1 X64 - Other Example Scripts

Share this post


Link to post
Share on other sites
Yaerox

Sorry for not replying 2 days. I was pretty busy in school ...

Well your script is showing me exactly what I already got:

Linux COMPUTERNAME 2.6.31-22-generic-pae #63-Ubuntu SMP Wed Aug 18 23:57:18 UTC 2010 i686
 
To access official Ubuntu documentation, please visit:
http://help.ubuntu.com/
 
  System information as of Mo 13. Jan 15:35:53 CET 2014
 
  System load:  0.0               Processes:           77
  Usage of /:   47.5% of 9.15GB   Users logged in:     1
  Memory usage: 9%                IP address for eth2: HOST
  Swap usage:   0%
 
  Graph this data and manage this system at https://landscape.canonical.com/
 
Last login: Mon Jan 13 15:23:36 2014 from MY_IP_ADRESS

 

 

Besides the fact that I got no idea how I should be able to use plink like this because the window is closing (if not running from command-prompt) which means I cant execute statements after connection ...

Anyone any idea? :/

 

Share this post


Link to post
Share on other sites
Yaerox

Got this running....I'll post the solution later, first I have to get my application finished now ...

#Edit: Solution (Console.au3 helped me to get this done):

Local $iPID = Run('"C:\Program Files (x86)\Putty\plink.exe" -ssh -pw PASSWORD USER@HOST:PORT', "", @SW_HIDE, 7)
For $i = 0 To 3
Local $iTimeout = WinWait("C:\Program Files (x86)\Putty\plink.exe", "", 10)
If $iTimeout <> 0 Then
ExitLoop
Else
If $i = 3 Then Exit
EndIf
Next
 
$iRval = __PlinkExpect($iPID, "USER@")
If $iRval = 0 Then
MsgBox(48, "", 'PuTTY - Es konnte keine erfolgreiche verbindung aufgebaut werden.')
Exit
EndIf
StdinWrite($iPID, "WHATEVERUWANT" & @CR)
 
ProcessClose($iPID)
 
Func __PlinkExpect($iPlink_PID, $sPlink_Match, $iTimeout = 30000)
Local $sText
Local $iRval
Local $iTimer = TimerInit()
Local $iTimerDiff
 
While 1
$iTimerDiff = TimerDiff($iTimer)
If $iTimerDiff > $iTimeout Then Return 0
 
$sText = StdoutRead($iPlink_PID)
$iRval = StringRegExp($sText, $sPlink_Match)
If $iRval = 1 Then ExitLoop
 
Sleep(100)
Wend
 
Return 1
EndFunc
Func _PlinkExpect($iPlink_PID, $sPlink_Match)
Local $iRvl = 0
 
While 1
$iRval = __PlinkExpect($iPlink_PID, $sPlink_Match)
If $iRval = 1 Then ExitLoop
 
Sleep(10000)
WEnd
EndFunc
Edited by MikeWenzel

Share this post


Link to post
Share on other sites
domagaj

Hi, 
Last 2 weeks I have searched many forums and i haven't found the answer for the question:
How to get all command output to Putty title?
Needed it for AutoIT to know when some jobs on a server is done and is it done right or wrong. Plink stdout and stdin wasn't working, I used many tweaks with wait delays and for some commands they worked and for others not. XSEL and XCLIP couldn't be installed on that server.

So here is the solution:
1. Get command output in a file.
2. Echo that file to title.

on putty client and suse server it looks like this:

ls /home | grep domagaja > logautoit.txt 
echo -e "\033]0\\;$(cat logautoit.txt)\\007\\c"

Hopefully someone will be able to use this. It won't work for all server types and putty client settings, of course, but the idea should work well.
Best
Jacek

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Similar Content

    • SkysLastChance
      By SkysLastChance
      I must be missing something. I have had this problem multiple times. Why does F6 key not send 6 times?  
      Not working 
      ControlSend("[CLASS:#32770]", "", "", ("{F6 6}")) Working
      ControlSend("[CLASS:#32770]", "", "", ("{F6}")) ControlSend("[CLASS:#32770]", "", "", ("{F6}")) ControlSend("[CLASS:#32770]", "", "", ("{F6}")) ControlSend("[CLASS:#32770]", "", "", ("{F6}")) ControlSend("[CLASS:#32770]", "", "", ("{F6}")) ControlSend("[CLASS:#32770]", "", "", ("{F6}"))  
    • AnonymousX
      By AnonymousX
      So I just got a new computer, and instantly went and installed Autoit onto it. I selected run script as default action when clicking on an au3 file, however every time I do this it opens the script in the editor. I can from there run the script no problem.
      I've tried reinstalling and checking the settings by hitting "Ctrl+1" and have confirmed it is set to run, but yet it just continues to open in the editor.
      Any ideas why this is happening or how to solve?
      New system is windows 10, old was windows 7
    • faustf
      By faustf
      hi guys
      i have a script  like this,  in windows 7 work perfect , in windows 10 not  work  , some one can explain me  why ??'
      #include <Array.au3> #include <MsgBoxConstants.au3> Global $aGPathFoto= FileReadToArray (@ScriptDir & "\TEMP\PathFoto.txt") Local $ctrl = 0 While 1 Local $hWnd = WinWait("[CLASS:#32770]", "", 10) If WinExists("[CLASS:#32770]") Then If $ctrl < 2 Then ControlSend($hWnd, "Choose File to Upload", "[CLASS:Edit; INSTANCE:1]", $aGPathFoto[0]&"{Enter}") Exit $ctrl += 1 Else ExitLoop EndIf EndIf WEnd $aGpathfoto = C:\Users\Public\Pictures\Sample Pictures\Chrysanthemum.jpg
    • HariKara
      By HariKara
      Hi AutoIters!
       
      Im trying to launch a .exe file that is nested within the program files (x86) folder structure. i have already used the standard RunAs Syntax and found that it fails to launch the application. I have switched to Run and that seems to work. My issue is I have to use RunAs as the applicaton would need to run under a completely different account. The Current logged in user is a Local User on the machine, however, the application must be run as a domain user. The Machine is domain connected.
       
       have tried the following:
      RunAs("username","logonpassword", $RUN_LOGON_PROFILE, "D:\Program Files (x86)\Vendor\Application Name\Exe Location\Executable.exe") The above fails to launch, there are no errors or syntax issues, it just does nothing when the variables are replaced for the correct values.
      I did the same using the Run command
      Run("D:\Program Files (x86)\Vendor\Application Name\Exe Location\Executable.exe") That seems to work fine, but runs in local user context. Any thoughts? Could it be a local Machine rights issue? Or have i missed something glaring in my script
    • Blueman
      By Blueman
      Hey Guys,

      I have a (i think) simple question, but i can't seem to get the answer though the help-files.
      Hope that you can help me with this issue.
      - I am opening a SSH Connection with the following function
      Func _Connect($session,$usr,$pass) $exec = @ScriptDir & "\PLINK.EXE" If Not FileExists($exec) Then _Err("PLINK.EXE Not Found!",0) $pid = Run($exec & " -load " & $session & " -l " & $usr & " -pw " & $pass, @ScriptDir, @SW_HIDE, 0x1 + 0x8) ;Run SSH.EXE If Not $pid Then _Err("Failed to connect",0) $currentpid = $pid ;$rtn = _Read($pid) ;Check for Login Success - Prompt ;MsgBox(48,"","1") ;sleep(5000) ;Wait for connection Return $pid EndFunc - This will connect to a CMS Server with a vt100 interface where a dynamic report is generated every 20 seconds.
      - Then i will read the contents with the following Function
      Func _Read($pid) $SSHREADTIMEOUT = 0 If Not $pid Then Return -1 Local $dataA Local $dataB Do $SSHREADTIMEOUT += 1 $dataB = $dataA sleep(100) $dataA &= StdOutRead($pid) If @error Then ExitLoop Until ($dataB = $dataA And $dataA And $dataB) OR $SSHREADTIMEOUT == 50 Return $dataA EndFunc This all goes correctly, but i can only read the contents once.
      When i try to read the contents again i get nothing.
      Maybe because the CMS isn't changing, but the values in the report is changing every 20 seconds.
      I need to somehow read al of the contents every time i perform a Read action, but how?
      Yes, i can use it in a While loop, but also then i get nothing or a small line of text and not the whole report.
       
      Any Idea?
       
      Thanks Guys!
       
      --Edit--
      I have fixed the problem by changing the terminal session to a vt220 session.
      The only problem now is that i want to send the: "Data link escape" command and that is something i cannot fix,.
      I have tried;
      StdinWrite($Pid,Hex(0x10)) StdinWrite($Pid,Chr(16)) StdinWrite($Pid,{DLE}) But nothing seems to work.  
      -- Edit-2 --
      Guys, fixed that too!
      Forgot to add the number '5' to actually execute the assignment.
      So fixed it by using;
       
      StdinWrite($Pid,Chr(16)) StdinWrite($Pid,"5")
      Thanks for reading with me
       
×