Python Forum
Pexpect help needed - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Pexpect help needed (/thread-7919.html)



Pexpect help needed - jash - Jan-30-2018

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!


RE: Pexpect help needed - micseydel - Jan-30-2018

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


RE: Pexpect help needed - jash - Jan-30-2018

#!/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