[Tutor] Help with pexpect

Nathan Farrar nathan.farrar at gmail.com
Fri Oct 16 21:45:29 CEST 2009

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:'])
print session.read() # shouldn't this display the logon banner & command prompt?

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!

"The presence of those seeking the truth is infinitely to be preferred
to the presence of those who think they've found it."

–Terry Pratchett

More information about the Tutor mailing list