Python Forum

Full Version: Pexpect help needed
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I'm new to Python programming and I'm trying to ssh using the pexpect code but it fails when I enter the password. Can someone please look into it. Also, Is there a tutorial for pexpect for automating the networking. Please suggest!
Thanks!

#!/usr/bin/env python
import pexpect

ssh_newkey = 'Are you sure you want to continue connecting'
# my ssh command line
p=pexpect.spawn('ssh [email protected] uname -a')

i=p.expect([ssh_newkey,'Password:',pexpect.EOF])
if i==0:
    print "I say yes"
    p.sendline('yes')
    i=p.expect([ssh_newkey,'password:',pexpect.EOF])
if i==1:
    print "I give password",
    p.sendline(password)     //// fails here
    p.expect(pexpect.EOF)    
elif i==2:
    print "I either got key or connection timeout"
    pass
print p.before # print out the result
Error:
Error: File "test/ssh3.py", line 15 p.sendline(pasword) ^ SyntaxError: invalid syntax

This is the entire program. I'm trying to check if I can ssh into the device successfully or not. Thanks!
Doing all the sane things I can think of, I can't figure out why you're getting a syntax error with this code. Please copy-paste your own code, remove the /// thing, and verify that you're really getting this issue with the code you've posted (rather than some other code).
#!/usr/bin/env python
import pexpect

ssh_newkey = 'Are you sure you want to continue connecting'
# my ssh command line
p=pexpect.spawn('ssh [email protected] uname -a')

i=p.expect([ssh_newkey,'Password:',pexpect.EOF])
if i==0:
    print "I say yes"
    p.sendline('yes')
    i=p.expect([ssh_newkey,'password:',pexpect.EOF])
if i==1:
    print "I give password",
    p.sendline(admin123)     
    p.expect(pexpect.EOF)    
elif i==2:
    print "I either got key or connection timeout"
    pass
print p.before # print out the result
Error:
python test/ssh3.py File "test/ssh3.py", line 15 p.sendline(admin123) ^ SyntaxError: invalid syntax You have mail in /var/spool/mail/root