Reputation: 1817
I'm writing a small piece of code to telnet into a switch and give username and password. I use pexpect spawn class for achieving this.
i have created a prompt_list with looking out for 'login' and 'password' and then enter username and password.
the problem that i see is pexpect matches 'login' but not the password. after running it for 2 -3 times, then it matches. do i have to add some delay or something like that to make it work on the first time.
can someone kindly help...
try:
child = pexpect.spawn(cmd, timeout= 100)
child.logfile = sys.stdout
child.sendline('\n')
conn = True
except:
print ' some exception occured'
if conn:
i = child.expect(prompt_list, timeout = 10)
if i == 0:
print 'inside login prompt'
child.sendline('admin')
i = child.expect(prompt_list, timeout = 10)
if i == 1:
print 'Inside password prompt'
child.sendline('password')
i = child.expect(prompt_list, timeout = 10)
my prompt_list is:
prompt_list = ['login:','Password:']
when i run this, i get the following error
Switch login: inside login prompt
admin
the prompt that i get when i login to switch manually is as follows.
switch login: admin
Password:
Upvotes: 1
Views: 1053
Reputation: 180401
Your child.sendline('\n')
in the try/except
is causing a problem:
import pexpect
conn = False
import sys
try:
child = pexpect.spawn("telnet 127.0.0.1", timeout= 100)
child.logfile = sys.stdout
conn = True
except:
print ' some exception occured'
if conn:
child.expect(":", timeout = 10)
child.sendline('user')
child.expect(":", timeout = 10)
child.sendline('password')
child.expect(">")
Upvotes: 1