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

    • nacerbaaziz
      By nacerbaaziz
      Hello
      i have searched a lot about how to run external programs with the administrator privileges without the script running with administrator privileges using autoit
      But all my attempts failed
      Finally, today I found the VBS function doing this task
      I immediately transferred it to our beloved language (autoit) and i decided to share it with you
      i hope you like it
      This is a simple example about how to use the function

      runAsAdmin("cmd.exe", "/c @echo off & cls & echo test & pause", "c:\", @sw_show)
      this is the function

      func runAsAdmin($program, $parameters = "", $workingDir = @workingDir, $show = "") local $oShell = OBJCreate("Shell.Application") if isOBJ($oShell) then local $result = $oShell.ShellExecute($program, $parameters, $workingDir, "runas", $show) else $result = false endIf return $result endFunc
       
    • rudi
      By rudi
      Hello,
      to provide an easy to use starter to capture traffic on all NICs found, I can successfully get all the interfaces of TSHARK.EXE (the command line version that's automatically installed along with wireshark) with this script:
      #include <AutoItConstants.au3> #include <Array.au3> $TS_WD = "C:\Program Files\Wireshark" $TS_exe = $TS_WD & "\tshark.exe" if not FileExists($TS_exe) Then MsgBox(48,"Fatal Error","No Wireshark Commandline Tool ""TSHARK.EXE"" found:" & @CRLF & _ $TS_exe) Exit EndIf $DString = "" $PIDGetIFs = Run($TS_exe & " -D", $TS_WD, @SW_HIDE, $STDERR_MERGED) While ProcessExists($PIDGetIFs) $DString &= StdoutRead($PIDGetIFs) WEnd ; MsgBox(0,"IFs",$DString) $aNICs = StringSplit($DString, @CRLF, 1) _ArrayDisplay($aNICs) $RegExIF = "^(?:\d+\. )(\\.*?})(?: \()(.*?)(\))$" ; $1 = TSHARK Interface Name, $2 = Windows Interface Name ; ... get the names to run TSHARK with the appropriate interface string  
      When I run TSHARK.EXE using this line directly, I see a continuously growing number telling the number of packets captured so far.
      "C:\Program Files\Wireshark\tshark.exe" -i \Device\NPF_{AEB931E9-E5FA-4DA5-8328-D87BDF53805C} -b duration:300 -b files:600 -w "y:\TShark-Ringbuffer\LAN-Verbindung\TSHARK-Com-0317_17---LAN-Verbindung___.pcap" Using this script, I *DO* see the first output line "Capturing on 'LAN-Verbindung'", but I cannot get hold of the continuously growing number of packets captured so far.
      #include <AutoItConstants.au3> $WD="C:\Program Files\Wireshark" $CMD='"C:\Program Files\Wireshark\tshark.exe" -i \Device\NPF_{AEB931E9-E5FA-4DA5-8328-D87BDF53805C} -b duration:300 -b files:600 -w "y:\TShark-Ringbuffer\LAN-Verbindung\TSHARK-Com-0317_17---LAN-Verbindung___.pcap"' ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $CMD = ' & $CMD & @CRLF & '>Error code: ' & @error & @CRLF) ;### Debug Console $PID=Run($CMD,$WD,@SW_SHOW,$stderr_merged) ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $PID = ' & $PID & @CRLF & '>Error code: ' & @error & @CRLF) ;### Debug Console $OutputAll="" While ProcessExists($PID) $output=StdoutRead($PID) $OutputAll&=$output ToolTip($OutputAll) if $output <> "" then ConsoleWrite("""" & $output & """" & @CRLF) Sleep(1000) WEnd ConsoleWrite("Process vanished" & @CRLF)  
      This is the output of SciTE, when I let TSHARK.EXE run for a short while, the "close" it's "box" ...
       
      --> Press Ctrl+Alt+Break to Restart or Ctrl+Break to Stop @@ Debug(10) : $CMD = "C:\Program Files\Wireshark\tshark.exe" -i \Device\NPF_{AEB931E9-E5FA-4DA5-8328-D87BDF53805C} -b duration:300 -b files:600 -w "y:\TShark-Ringbuffer\LAN-Verbindung\TSHARK-Com-0317_17---LAN-Verbindung___.pcap" >Error code: 0 @@ Debug(13) : $PID = 10948 >Error code: 0 "Capturing on 'LAN-Verbindung' " Process vanished  
      Howto catch the "growing-packet-number" TSHARK.EXE is writing continuously to the same "window position"???
       
       
      Regards, Rudi.
       


    • Fortitude
      By Fortitude
      Hello!
      I have made a program, that opens a folder, and then store the path ("C:\programFiles\etc") in a variable.
      Then i need to open the command panel as administrator, and write  in the stored path, and some other text.

      Can i somehow instantly copypaste the text i needed, instead of writing in the command panel with the Send commands?
       
      #RequireAdmin #include <MsgBoxConstants.au3> Global Const $sMessage = "Select a folder" ; Display an open dialog to select a file. Global $sFileSelectFolder = FileSelectFolder($sMessage, "") If @error Then MsgBox($MB_SYSTEMMODAL, "", "Nem választottál ki mappát!") Exit EndIf Global $Tiff_path = $sFileSelectFolder & "\TiffChecker_2_32" FileCopy( "C:\TiffChecker_2_32.exe" , $sFileSelectFolder) $CMD = $Tiff_path & " " & $sFileSelectFolder & " /s/d" Run("C:\WINDOWS\system32\cmd.exe") WinWaitActive("C:\WINDOWS\system32\cmd.exe") Send($CMD) Sleep(1000) Send("{ENTER}") sleep(1000) MsgBox(0,"Press OK", "Press OK to continue") FileCopy("C:\Tiffchecker.txt", $sFileSelectFolder) Also, how do i close the command panel, when the program is finished? (usually its under 1 second, so after the send i just put a Sleep function there)
    • PoojaKrishna
      By PoojaKrishna
      Hi friends,
      The Run command on my machine suddenly stopped execution.
      Anyone of you facing the same issue?
      #include <AutoItConstants.au3> #include <FileConstants.au3.> #RequireAdmin $ExeName = FileOpenDialog("Select AutoIt executable", @ScriptDir & "\", "(*.exe)", $FD_FILEMUSTEXIST) If not @error Then If FileExists($ExeName) Then $iPID = Run($ExeName, "", @SW_HIDE, $STDIN_CHILD + $STDOUT_CHILD) If @error Then msgbox(0,"Failed", "Failed execution with @error: " & @error) EndIf Else Msgbox(0,"", "File does not exist") EndIf EndIf The above code always fails to execute the executable file. It was working perfect and stopped working now.
      I have tried adding the files and folders to windows defender exclusion list and uninstalling the anti virus but still no luck.
      OS: Windows 10 - 64 bit
      Please help.
       
    • Docfxit
      By Docfxit
      I would like to know if there is a way to make the send cmd send characters when using
      Graphical AutoIt Debugger By Stumpii,
      If Not WinActive("Setup - UltraVNC","Setup is preparing to install") Then WinActivate("Setup - UltraVNC","Setup is preparing to install") WinWaitActive("Setup - UltraVNC","Setup is preparing to install") Send("{ALTDOWN}f{ALTUP}") ;Finish The debugger when using step mode will execute the Send cmd line but the window doesn't receive the input.
      Thanks,
      Docfxit
×