Jump to content

Reading from PuTTy or DOS prompt

Recommended Posts

I've been using AutoIT for a while now and find it quite useful. I taught myself how to program with it, however, one obstacle I have not been able to overcome is how to get text from command promps. More specifically from a program called PuTTy.

I am developing an automated device checking tool for my company, and it involves opening PuTTy to run an SSH connection to various *nix servers to check and see if the device is in use or not from a BASH prompt. The AutoIT Window Info tool does not detect the command screen, so I'm not sure how to capture the reply messages I am receiving. I've tried someing like this, but it doesn't work. Any suggestions?

This is what I tried, and then had to change to a msgbox for the user to check and deccide whether to continue or not.

;$winText = WinGetText ( "***.***.***.*** - PuTTY", "" )
;If StringInStr ( "$winText", "No rows selected.", 0, 1 ) <> 0 then

 $reply = MsgBox( 4, "Proceed?", "Proceed with insert?" )
   if $reply = 6 then 
Edited by AutoITPimp
Link to comment
Share on other sites

The only thing I can help you with, is this:

;If StringInStr ( "$winText", "No rows selected.", 0, 1 ) <> 0 then

Should be:

;If StringInStr ( $winText, "No rows selected.", 0, 1 ) <> 0 then

Noticed I removed the """ around the variable $winText... Even though it's a comment, it should still be right :dance: But I don't think this will get you far, someone else can probably help. :whistle:

Edited by layer
Link to comment
Share on other sites

Actually, that was just a typo in my post. I tried it this time on another script and it seems to work. I think my problem is that I was actually trying to use ControlGetText function. =P

But, is there a better way to read this, or to know when a reply comes back from the server?

**EDIT: After some checking and playing with the function it appears it does not work. The AutoIT Window Info tool does not show any window text for command prompts or PuTTy, and when MsgBox for the text received from WinGetText, it displays an empty MsgBox. As I recall, this is the problem I have had in the past.

Edited by AutoITPimp
Link to comment
Share on other sites

The AutoIt beta had functions for reading output and writing input directly from console apps, rather than scraping the text from a window: StdoutRead, StdinWrite, etc.

I've used Plink, one of the PuTTY tools, to run a SSH session using the functions above, but it's not pretty; there's no terminal per se so you get ANSI control characters and junk in the strings that you read.

Yes yes yes, there it was. Youth must go, ah yes. But youth is only being in a way like it might be an animal. No, it is not just being an animal so much as being like one of these malenky toys you viddy being sold in the streets, like little chellovecks made out of tin and with a spring inside and then a winding handle on the outside and you wind it up grrr grrr grrr and off it itties, like walking, O my brothers. But it itties in a straight line and bangs straight into things bang bang and it cannot help what it is doing. Being young is like being like one of these malenky machines.

Link to comment
Share on other sites

Hello AutoITPimp,

I´am not sure if this helps in your situation but I wrote once a script to retreive my current public IP by logging into my router via telnet. I was able to send something to the command prompt but not getting text from it, so I redirected the output to a file and parsed that. Here is what I wrote (quite old, one of my first autoIt scripts).

AutoItSetOption("WinDetectHiddenText", 1)

; ----------------------------------------------------------------------------
; Script Start
; ----------------------------------------------------------------------------

Run ( "telnet -f " &@TempDir & "\routerip.log" , "e:", @SW_HIDE )
ControlSend ( "Telnet", "", "", "password")
ControlSend ( "Telnet", "", "", "show ip{ENTER}")
$var = ControlGetText("Telnet", "", "ConsoleWindowClass")
ControlSend ( "Telnet", "", "", "log{ENTER}{ENTER}")
$file = FileOpen(@TempDir & "\routerip.log", 0)
If $file = -1 Then
    MsgBox(0, "Error", "Unable to open file.")

While 1
   $line = FileReadLine($file)
   If @error = -1 Then ExitLoop
   IF (StringInStr ( $line, "ewanisp")) Then
      $line = StringMid ( $line, 33, 15 )
      MsgBox(0, "Aktuelle IP", $line)

FileDelete (@TempDir & "\routerip.log")

Don´t know if this is of any use.


Link to comment
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

  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Create New...