Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Pexpect help needed
#1
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!
Reply
#2
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).
Reply
#3
#!/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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Reading and storing a line of output from pexpect child eagerissac 1 4,144 Feb-20-2024, 05:51 AM
Last Post: ayoshittu
  Use pexpect to send user input alisha17 0 1,826 May-10-2022, 02:44 AM
Last Post: alisha17
  Not able to read the text using pexpect/expect Bipinjohnson 7 3,938 Jan-10-2022, 11:21 AM
Last Post: Bipinjohnson
  Sudden Problem with pexpect gw1500se 3 2,322 Nov-19-2021, 11:21 PM
Last Post: bowlofred
  How to use pexpect in python? tiho_bg 1 1,488 Oct-30-2021, 02:50 PM
Last Post: Yoriz
  Pexpect timesout before executing whole output eagerissac 0 1,451 Jun-23-2021, 03:30 AM
Last Post: eagerissac
  pexpect startup help korenron 2 3,429 Apr-27-2021, 07:23 AM
Last Post: korenron
  Problem with pexpect.exception.TimeOUT korenron 0 3,240 Apr-12-2021, 03:25 PM
Last Post: korenron
  Request help on pexpect rsurathu 4 5,721 Jul-19-2020, 03:35 PM
Last Post: snippsat
  SIGWINCH ignored when trying to start a screen with the pexpect module StatTark 0 1,791 Jul-06-2020, 10:12 AM
Last Post: StatTark

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020