Sign in to follow this  
Followers 0
sleepy

How can I read data from an oppen window?

3 posts in this topic

From Harry Nash

New user!

Hello, I use autoit to access an open putty ssh shell as well as windows command terminals. I am

able to send in the window a command I just can find a receive function or read from terminal fuction.

Currently I am sending commands to a lynux system that runs a test, I won't know when it

is finished. I tried winGetText I only see a 0 returned.

Share this post


Link to post
Share on other sites



#3 ·  Posted (edited)

I was having a hard time finding easy to understand info (for a newbie) for how to send and receive stuff from a SSH session, so I decided I would post this for other newb's like me to find.

This is how I ended up handling it. I used plink.exe (command line version of PuTTY)

NOTE: This is by no means, necessarily the best way. But easy to understand and use for newb's. IMHO

;this runs plink in a command window, and provides a handle that can be used for Stdin, Stdout, and Stderr 
$plinkhandle = Run(@comspec & ' /c x:\plink.exe -ssh 192.168.202.231 ','',@SW_HIDE,7)


$username = InputBox("username","Enter username")
$password = InputBox("password","Enter password",'','*')

;waits for "login"
While 1
    $text = StdoutRead($plinkhandle)
    $oktogo = StringRegExp($text,".*login.*")
    If $oktogo = 1 Then ExitLoop
Wend

;For some reason I had to put a space after each because the last character was being cut off.  
;But only on the login part, after that it worked fine with no extra space. 

StdinWrite($plinkhandle, $username & " " & @CR)

;waits for "password"
While 1
    $text = StdoutRead($plinkhandle)
    $oktogo = StringRegExp($text,".*password.*")
    If $oktogo = 1 Then ExitLoop
Wend

;For some reason I had to put a space after each because the last character was being cut off. 
;But only on the login part, after that it worked fine with no extra space. 

StdinWrite($plinkhandle, $password & " " & @CR)


;I have some stuff in my login script that I normall press enter (to accept default) a few times to get to my actual prompt
;so this block is just hitting enter until I get to /home/username

;note: if you use a while block that keeps Stdoutread'ing it splits up all the output 
;If you for instance wanted to see all what was displayed during the while block you 
;could un-comment the lines of code below - to pile it into $fulltext, each time you read

;Dim $fulltext
While 1
    StdinWrite($plinkhandle, @CR)                            
    Sleep(100)
    $text = StdoutRead($plinkhandle)
;$fulltext = $fulltext & @CRLF & $text
    $oktogo = StringRegExp($text,".*home.*")
    If $oktogo = 1 Then ExitLoop
Wend
;MsgBox(0, "Display", $Fulltext)


;I had to set terminal to "dumb" to make it work well

StdinWrite($plinkhandle,"setenv TERM dumb" & @CR)

While 1
       $text = StdoutRead($plinkhandle)
    $oktogo = StringRegExp($text,".*home.*")
       If $oktogo = 1 Then ExitLoop
Wend

StdinWrite($plinkhandle,"cd ./scripts" & @CR)
Sleep(500)

$text = StdoutRead($plinkhandle) 
;I do this to "clear" the output - otherwise, when I do it below 
;to see the output of my script - it includes the typing of, and the output from the cd ./scripts too. 


StdinWrite($plinkhandle,"script.to.run.csh" & @CR)
Sleep(1000) 

;As a side note I noticed, I needed to sleep for about 100-1000 after StdinWrite before using StdoutRead
;(even for something nearly instant like "cd ./scripts" and having a while block to wait for the prompt)
;or else it did not catch the output from that latest command. 

$text = StdoutRead($plinkhandle)
MsgBox(0, "Output from script.to.run.csh", $text)

;note: since I just sleep'd instead of a while loop Stdoutread'ing all the 
;mutli line output "piled up" and could be ready with a single Stdoutread. 
;This will also include the prompt your left at after the command runs.  
;I don't yet know easy wat to cut this off, because I dont care enough for my purposes
;to figure it out

StdinWrite($plinkhandle,"logout" & @CR)

;It's also worth noting that 
;$text = StdoutRead($plinkhandle)
;when there is no new data in Stdout seems to lock-up the script
Edited by bdb4269

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
Sign in to follow this  
Followers 0