Python 2.7 使用Pexpect捕获多个命令输出
我正在使用pexpect为cisco设备编写脚本Python 2.7 使用Pexpect捕获多个命令输出,python-2.7,pexpect,Python 2.7,Pexpect,我正在使用pexpect为cisco设备编写脚本 import pexpect ssh_command = ' ' uname = ' ' pass = ' ' fout = file('logfile.txt','w') child = pexpect.spawn (ssh_command) print 'ssh initiated' child.expect('.*name:') child.sendline(uname) child.expect('.*assword:') child.s
import pexpect
ssh_command = ' '
uname = ' '
pass = ' '
fout = file('logfile.txt','w')
child = pexpect.spawn (ssh_command)
print 'ssh initiated'
child.expect('.*name:')
child.sendline(uname)
child.expect('.*assword:')
child.sendline(pass)
child.expect('#')
child.sendline('sh ip int br')
child.logfile = fout
child.expect('#')
print(child.before)
child.sendline('sh hardware')
child.expect(['#',pexpect.EOF])
child.logfile = fout
fout.close()
出于某种原因,它发出第一个命令,我可以看到接口的输出,但是脚本挂在那里。当我退出脚本时,它会抛出一个错误
child.expect(['#',pexpect.EOF])
我似乎不明白它为什么停在那里
编辑
回溯:
Traceback (most recent call last):
File "helloworld.py", line 18, in <module>
child.expect('#')
回溯(最近一次呼叫最后一次):
文件“helloworld.py”,第18行,在
child.expect(“#”)
当我在没有打印(child.before)的情况下运行此命令时,我看不到输出,但它可以工作!它会写入文件
当我包含
print(child.before)
命令时,为什么脚本挂起?无关:child.expect('.*pattern')
与刚才的child.expect('pattern')
查看为什么会看到输出?它不应该被pexpect
捕获吗?提供完整的回溯和实际代码(),即除了用户名和密码之外的实际代码。它打印出第一个命令的输出,但不执行第二个命令。我在回溯中没有看到pexpect.EOF
。此外,仍然看不到完整的回溯,例如,异常的类型是什么?