<div class="gmail_quote">On Fri, Oct 16, 2009 at 1:45 PM, Nathan Farrar <span dir="ltr"><<a href="mailto:nathan.farrar@gmail.com">nathan.farrar@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
I'm trying to automate the collection of data to remote devices over<br>
ssh via pexpect. I had originally attempted (with limited success) to<br>
use paramiko, however due to cisco's ssh implimentation I cannot send<br>
mulitple commands over the same connection, which is absolutely<br>
essential. Therefore, I'm now attempting to use pexpect, but am<br>
having trouble getting started. I have a simple script, such as:<br>
<br>
session = pexpect.spawn('ssh user@host')<br>
session.expect([pexpect.TIMETOUT, 'password:'])<br>
session.send('password')<br>
print session.read() # shouldn't this display the logon banner & command prompt?<br>
session.close()<br>
<br>
This code results in no output & a hanging window. What am I doing incorrect?<br>
<br>
Additionally, I will need to add conditional pexpect statements, such<br>
that when I execute a command I want to gather the output, however if<br>
'--More--' is encountered (and it this can happen multiple times), I<br>
want to send a newline character to the session and continue gathering<br>
the output.<br>
<br>
Thanks for the help!<br>
Nathan<br>
<br>
--<br>
"The presence of those seeking the truth is infinitely to be preferred<br>
to the presence of those who think they've found it."<br>
<br>
–Terry Pratchett<br>
_______________________________________________<br>
Tutor maillist - <a href="mailto:Tutor@python.org">Tutor@python.org</a><br>
To unsubscribe or change subscription options:<br>
<a href="http://mail.python.org/mailman/listinfo/tutor" target="_blank">http://mail.python.org/mailman/listinfo/tutor</a><br>
</blockquote></div><br>when using pexpect for ssh you can run into some issues, I have used to script some elaborate green screen apps, but not without some unforeseen bugs <br><br>when connecting to a new machine, most ssh client will ask you verify the fingerprint<br>
<br>something like: <br><br>> are you sure you want to continue (yes/no)?<br><br>also session.send < doesn't send eol "\n" but sendline does<br><br>So this should fix your connection<br><br>###<br>session = pexpect.spawn('ssh user@host')<br>
i = session.expect([pexpect.TIMETOUT, 'password:', "yes/no"])<br>if i == 2:<br> session.sendline("yes")<br> session.expect([pexpect.TIMETOUT, 'password:'])<br>if i == 1:<br> session.sendline('password')<br>
print session.read() # shouldn't this display the logon banner & command prompt?<br>session.close()<br>###<br><br>the other your question, pexpects sendline(''), will just send a newline <br><br>not perfect but should work<br>
###<br>license = ""<br>while True:<br> i = session.expect(["--more--", "yes/no"])<br> if i == 1:<br> break<br> license += session.before<br> session.sendline('')<br>
###<br><br><br>Vince<br> <br><br><br><br>