我正在编写一小部分代码来告诉网络进入交换机,并给出用户名和密码。我使用pexpect派生类来实现这一点。
我创建了一个prompt_list,用于查找“登录”和“密码”,然后输入用户名和密码。
我看到的问题是pexpect匹配‘登录’,而不是密码。在运行2-3次之后,它就匹配了。我是否需要添加一些延迟或类似的东西,以使它在第一次工作。
有人能帮忙吗..。
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)我的prompt_list是:
prompt_list = ['login:','Password:']当我运行这个程序时,我会得到以下错误
Switch login: inside login prompt
admin当我登录手动切换时得到的提示如下所示。
switch login: admin
Password:发布于 2015-01-09 17:45:30
您的child.sendline('\n')在try/except中引起了一个问题:
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(">")https://stackoverflow.com/questions/27865421
复制相似问题