Jump to content

Expect with AutoIt?


Recommended Posts

Hi,

 

I wrote a script to launch putty as telnet and connect with user\password, it works, but the challenge I am facing now is how to limit the script to only continue once the following message appears in putty "login:" :

Welcome to Microsoft Telnet Service

login:

tJkocTb.png

The script:

 

#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_Outfile=test_cmd2.exe
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
;Global $ip
$ip = $CmdLine[1]
$username = $CmdLine[2]
$pwd = $CmdLine[3]
$port = ':23'

Run(@ScriptFullPath, 'c:\putty\putty.exe telnet://' & $ip & $port)
;Run(@ScriptFullPath &'c:\putty\putty.exe telnet://' & $ip & $port)
WinWait($ip & " - PuTTY")
; how do I add only continue if 'Login:' appears?
ControlSend($ip & " - PuTTY", "", "", $username & "{ENTER}")
ControlSend($ip & " - PuTTY", "", "", $pwd & "{ENTER}")

I tried to extract information I can use with autoIt windows info, but no luck for the cmd output.

 

Thanks!

Link to comment
Share on other sites

Set the opt_flag of the Run function to $STDOUT_CHILD. Then use StdoutRead to process the displayed lines.

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

35 minutes ago, water said:

Set the opt_flag of the Run function to $STDOUT_CHILD. Then use StdoutRead to process the displayed lines.

Thank you.

 

I am clearly doing something wrong.

this works:

#include <constants.au3>


$ip = '192.168.1.11'
$username = 'telnet'
$pwd = 'Aa123456'
$port = ':23'

Run(@ScriptDir &'\putty.exe telnet://' & $ip & $port)
WinWait($ip & " - PuTTY")
ControlSend($ip & " - PuTTY", "", "", $username & "{ENTER}")
ControlSend($ip & " - PuTTY", "", "", $pwd & "{ENTER}")

this doesnt:

 

#include <constants.au3>


$ip = '192.168.1.11'
$username = 'telnet'
$pwd = 'Aa123456'
$port = ':23'

Run(@ScriptDir &'\putty.exe telnet://' & $ip & $port, "","", $STDOUT_CHILD)
WinWait($ip & " - PuTTY")
ControlSend($ip & " - PuTTY", "", "", $username & "{ENTER}")
ControlSend($ip & " - PuTTY", "", "", $pwd & "{ENTER}")

I see that it opens task for putty.exe, but nothing is displayed on my screen.

also, how do I print everything $STDOUT_CHILD get, to make sure it is capturing it all?

 

Thanks again

Edited by kelso
Link to comment
Share on other sites

First:
Should be

Run(@ScriptDir & '\putty.exe telnet://' & $ip & $port, "", Default, $STDOUT_CHILD)


Second:
Use StdInWrite to send data to Telnet.

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

1 hour ago, water said:

First:
Should be

Run(@ScriptDir & '\putty.exe telnet://' & $ip & $port, "", Default, $STDOUT_CHILD)


Second:
Use StdInWrite to send data to Telnet.

it seems like $stdout_child is basically returning the PID of the executable whiten the run command.

I still fail to understand how will it log the data that is in the console?

also, is there a way to sample it and make sure it does capture every txt from putty.exe?

Local $process=Run(@ScriptDir & '\putty.exe telnet://' & $ip & $port, "", Default, $STDOUT_CHILD)
MsgBox(0, "data", $process)

 

 

 

Link to comment
Share on other sites

As I said in post #3 use StdoutRead to retrieve the output of putty.

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

45 minutes ago, water said:

As I said in post #3 use StdoutRead to retrieve the output of putty.

TRaDZMb.png

 

I would assume that it will log the highlighted part and print it in autoit editor no? it doesnt.

 

When I switch to ipconfig, it works:

local $out
;
$rslt = Run(@ComSpec & " /c netstat -an ", @SystemDir, @SW_HIDE, $STDERR_MERGED + $STDOUT_CHILD)
While 1
   $out = StdoutRead($rslt)
   If @error then exitloop
   if stringlen($out) > 0 then consolewrite($out & @lf)
WEnd

 

thoughts?

 

Link to comment
Share on other sites

2 hours ago, kelso said:

 

local $out
;
$rslt = Run(@ComSpec & " /c netstat -an ", @SystemDir, @SW_HIDE, $STDERR_MERGED + $STDOUT_CHILD)
While 1
   $out = StdoutRead($rslt)
   If @error then exitloop
   if stringlen($out) > 0 then consolewrite($out & @lf)
WEnd

 

thoughts?

 

Try this:
$STDOUT_CHILD + $STDERR_CHILD
And read the output like this:

While ProcessExists($cPID)
        $stdError = StderrRead($cPID)
        $stdOut &= StdoutRead($cPID)
WEnd
ConsoleWrite($stdOut&@CRLF)

Regards
Alien.

Link to comment
Share on other sites

6 minutes ago, alien4u said:

Try this:
$STDOUT_CHILD + $STDERR_CHILD
And read the output like this:

While ProcessExists($cPID)
        $stdError = StderrRead($cPID)
        $stdOut &= StdoutRead($cPID)
WEnd
ConsoleWrite($stdOut&@CRLF)

Regards
Alien.

Hi Alien,

 

Thanks for the response.

This is what I used:

 

#include <Constants.au3>

$cPID = Run(@ScriptDir &'\putty.exe telnet://192.168.1.11:23' , @SystemDir, $STDOUT_CHILD + $STDERR_CHILD)
While ProcessExists($cPID)
        $stdError = StderrRead($cPID)
        $stdOut &= StdoutRead($cPID)
WEnd
ConsoleWrite($stdOut&@CRLF)

I get:

"C:\putty\1\a.au3" (6) : ==> Variable used without being declared.:
$stdOut &= StdoutRead($cPID)
^ ERROR

 

What am I missing?

Link to comment
Share on other sites

1 hour ago, kelso said:

Hi Alien,

 

Thanks for the response.

This is what I used:

 

#include <Constants.au3>

$cPID = Run(@ScriptDir &'\putty.exe telnet://192.168.1.11:23' , @SystemDir, $STDOUT_CHILD + $STDERR_CHILD)
$stdOut = ""
While ProcessExists($cPID)
        $stdError = StderrRead($cPID)
        $stdOut &= StdoutRead($cPID)
WEnd
ConsoleWrite($stdOut&@CRLF)

I get:

"C:\putty\1\a.au3" (6) : ==> Variable used without being declared.:
$stdOut &= StdoutRead($cPID)
^ ERROR

 

What am I missing?

Just declare the variable first...like this:
 

#include <Constants.au3>
$cPID = Run(@ScriptDir &'\putty.exe telnet://192.168.1.11:23' , @SystemDir, $STDOUT_CHILD + $STDERR_CHILD)
$stdOut = ""
While ProcessExists($cPID)
        $stdError = StderrRead($cPID)
        $stdOut &= StdoutRead($cPID)
WEnd
ConsoleWrite($stdOut&@CRLF)

Don't pay... learn...

Regards
Alien.

Link to comment
Share on other sites

1 hour ago, alien4u said:

Just declare the variable first...like this:
 

#include <Constants.au3>
$cPID = Run(@ScriptDir &'\putty.exe telnet://192.168.1.11:23' , @SystemDir, $STDOUT_CHILD + $STDERR_CHILD)
$stdOut = ""
While ProcessExists($cPID)
        $stdError = StderrRead($cPID)
        $stdOut &= StdoutRead($cPID)
WEnd
ConsoleWrite($stdOut&@CRLF)

Don't pay... learn...

Regards
Alien.

Thank you for your continues help.

Here is where  I am at:

wSqxTpP.png

 

it runs, putty starts, I enter user name + password, authentication successful

then I click help to generate output within putty, but still don't see that in the console\editor (see above screenshot)

thoughts? 

Link to comment
Share on other sites

36 minutes ago, alien4u said:

If you read the help file for the Function Run() you will notice:
Run ( "program" [, "workingdir" [, show_flag [, opt_flag]]] )

This mean your command should be:

Run(@ScriptDir &'\putty.exe telnet://192.168.1.11:23', @SystemDir, @SW_MAXIMIZE, $STDOUT_CHILD + $STDERR_CHILD)

You where missing a parameter needed by Run(). show_flag.

Regards
Alien.

Thank you again my friend, but the result is still the same, this time with maximize screen.

 

#include <Constants.au3>
$cPID = Run(@ScriptDir &'\putty.exe telnet://192.168.1.11:23', @SystemDir, @SW_MAXIMIZE, $STDOUT_CHILD + $STDERR_CHILD)
$stdOut = ""
While ProcessExists($cPID)
        $stdError = StderrRead($cPID)
        $stdOut &= StdoutRead($cPID)
WEnd
ConsoleWrite($stdOut&@CRLF)

It runs well, but it doesnt log the output that I highlighted in the screenshot to the editor, how can I base my next actions on the output it gets to continue my script without it?

KFyc2Tw.png

 

I am looking to continue the script based on responses I get from the telnet, such as:

if "login:" enter username

if "password:" enter password

if "Enter the new time: quit

etc.

 

Thank you again, I really appreciate your help

 

Link to comment
Share on other sites

As I stated in post #4 use StdInWrite to send data (Your login etc.) to Putty. I'm not sure you can mix a script accessing StdIn/StdOut/StdErr and Console input.

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

11 hours ago, alien4u said:

What about this:
 

Run(@ComSpec & " /k " & @ScriptDir &'\putty.exe telnet://192.168.1.11:23', "", @SW_MAXIMIZE, $STDOUT_CHILD + $STDERR_CHILD)

If this does not work for you then you should use plink that is putty command line tool.

Regards
Alien.

I get the output with plink:

#include <Constants.au3>
$cPID = Run(@ComSpec & " /k " & @ScriptDir &'\plink.exe -telnet telnet@192.168.1.11', "", @SW_MAXIMIZE, $STDOUT_CHILD + $STDERR_CHILD)
$stdOut = ""
While ProcessExists($cPID)
        $stdError = StderrRead($cPID)
        $stdOut &= StdoutRead($cPID)
WEnd
ConsoleWrite($stdOut&@CRLF)

I get in the console:

--> Press Ctrl+Alt+Break to Restart or Ctrl+Break to Stop
Welcome to Microsoft Telnet Service 

password: 
C:\putty\1>

 

which is great, but two questions:

1) the plink window shows up and disappear really quick, why doesn't it stay on the password: until further instructions?

2) ok, now I got the output to the terminal, which functions do I use to base my actions upon that output?

 

if "password:" 

write in console 123456 {ENTER}

something like that.

 

Thanks!

 

Link to comment
Share on other sites

What did I write in post #4 and #17? Do you read what people post here :poke:

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

7 minutes ago, water said:

What did I write in post #4 and #17? Do you read what people post here :poke:

Thanks water. this explains how I continue to interact with the terminal, but why does the window disappear? any ideas?

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
 Share

  • Recently Browsing   0 members

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