[Tutor] Help with pexpect
Mark K. Zanfardino
mzanfardino at gmail.com
Fri Oct 16 22:43:35 CEST 2009
Nathan,
Depending upon how much control you have over the calling machine you
can eliminate the pesky password prompt from ssh altogether by creating
a private/public key pair for the client machine and copy the public key
to the host machine. This way when you ssh to the host you will be
authenticated based on the key pair. Check out ssh-copy-id.
Mark
On Fri, 2009-10-16 at 13:45 -0600, Nathan Farrar wrote:
> I'm trying to automate the collection of data to remote devices over
> ssh via pexpect. I had originally attempted (with limited success) to
> use paramiko, however due to cisco's ssh implimentation I cannot send
> mulitple commands over the same connection, which is absolutely
> essential. Therefore, I'm now attempting to use pexpect, but am
> having trouble getting started. I have a simple script, such as:
>
> session = pexpect.spawn('ssh user at host')
> session.expect([pexpect.TIMETOUT, 'password:'])
> session.send('password')
> print session.read() # shouldn't this display the logon banner & command prompt?
> session.close()
>
> This code results in no output & a hanging window. What am I doing incorrect?
>
> Additionally, I will need to add conditional pexpect statements, such
> that when I execute a command I want to gather the output, however if
> '--More--' is encountered (and it this can happen multiple times), I
> want to send a newline character to the session and continue gathering
> the output.
>
> Thanks for the help!
> Nathan
>
More information about the Tutor
mailing list