Jump to content

Plink and StdinWrite


egas
 Share

Recommended Posts

Good day everyone,

I am having some difficulty sending keystrokes to plink.

What I am trying to do is automate an SSH task to a Cisco device.

While I am not a scripting guru, I have some experience with writing scripts, however none with AutoIt.

$PPID = Run(@comspec & " /c" & "C:\Tools\Putty\plink.exe -ssh 10.10.10.10","",@SW_MAXIMIZE,7)
StdinWrite($PPID, "admin")
Sleep(5000)
Exit

Above is my exact code with the exception of the IP address. The plink window lauches, but it does not send the keystrokes. What am I missing?

Any feedback will be greatly appreciated. Thank you.

Link to comment
Share on other sites

It might be a timing issue.  Try putting in a delay before the StdInWrite command.

The way I did this (many years ago), is I put in a StdOutRead loop until I received successful login indication.

I always recommend responding based on feedback rather than a timed execution.

Link to comment
Share on other sites

I use plink like this to perform a grep:

$Connection = "plink.exe -ssh COMPUTERNAME -l USERNAME -pw PASSWORD"
$log_dir = @ScriptDir & "\" & @MON & @MDAY & @YEAR & "\"

$log = ">" & $log_dir & "\REMOTE_LOGIN.log"
$Command1 = "grep -i -P '(extraweb__authen HTTP)' /var/log/daily.log"
runwait('cmd /c ' & $Connection & ' ' & $Command1 & $log)

 

Edited by boththose

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Link to comment
Share on other sites

It might be a timing issue.  Try putting in a delay before the StdInWrite command.

The way I did this (many years ago), is I put in a StdOutRead loop until I received successful login indication.

I always recommend responding based on feedback rather than a timed execution.

@spudw2k, thanks for the feedback.

The loop to wait for feedback makes sense, but it doesn't that long to connect.

$PPID = Run("C:\Tools\Putty\plink.exe -ssh 172.19.190.2", "", @SW_SHOWDEFAULT)
Sleep(10000)
StdinWrite($PPID, "admin")
ProcessClose($PPID)
Exit

I read where @comspec is only needed to run internal commands like dir, so I switched to the the standard run function. I am able to get the command window, but it still isn't sending the keystrokes for admin.

plink4.thumb.PNG.4e6b709082f2c2be13fd4b7

Any other ideas?

Link to comment
Share on other sites

Hey @boththose, thanks for the feedback.

Unfortunately, the cisco box I'm trying to connect to does not work with the username and password switches.

plink5.thumb.PNG.6ae16b71ac029466831dda3

So what I concluded that I need to do is figure out a way to send keystrokes to plink and simulate the session.

 

Link to comment
Share on other sites

sux, ive only accomplished that via a putty session called from plink.

as described here in 7.2.1:

http://the.earth.li/~sgtatham/putty/0.60/htmldoc/Chapter7.html

Edited by boththose
found a link

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Link to comment
Share on other sites

You could just activate the console window then send key strokes directly to it.

WinActivate("Administrator:Command Prompt","<Cisco Controller>")
Send("Keystrokesgohere")

but stdin is probably not writing to standard io because  you haven't included $STDIN_CHILD in the run statement like this:
 

$PPID = Run("C:\Tools\Putty\plink.exe -ssh 172.19.190.2", "", @SW_SHOWDEFAULT,$STDIN_CHILD)

 

Link to comment
Share on other sites

Hey @gruntydatsun, thanks for the feedback.

I didn't include the $STDIN_CHILD, but reason was because the script was not running. However, I learnt that I needed to do this:

#include <Constants.au3>

Then I started seeing some progress! But it was short lived because as it would hang at the password prompt.

#include <Constants.au3>
;$PPID = Run(@comspec & " /c" & "C:\Tools\Putty\plink.exe -ssh 172.19.190.2","",@SW_SHOWDEFAULT,7)
$PPID = Run("C:\Scripts\plink.exe -ssh wlcontrol", "", @SW_SHOWDEFAULT, $STDIN_CHILD)
Sleep(10000)
StdinWrite($PPID, "first_prompt" & @CRLF)
Sleep(5000)
StdinWrite($PPID, "second_prompt"& @CRLF)
Sleep(15000)
StdinWrite($PPID, "password"& @CRLF)
Sleep(30000)
StdinWrite($PPID, "ping 172.16.1.169"& @CRLF)
Sleep(1000)
StdinWrite($PPID, "logout "& @CRLF)
Sleep(5000)
ProcessClose($PPID)
Exit

This is the output.

 

plink6.thumb.PNG.1efbb9d06a5d5c7256d08f8

 

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...