所以我一直在努力解决这个问题,我似乎无法找到解决方案。 我正在telnet到cisco 7200路由器并试图获得几个BGP连接的状态,并且由于某种原因我只是得到了提示的开头。有关代码中注释失败的更多信息。
child = pexpect.spawn('telnet %s' % (host.gethostname()), maxread=16384)
#child.logfile = sys.stdout
#child delaybeforesend = 0
child.timeout = 200
child.expect('Username:')
child.sendline(username)
child.expect('Password:')
child.sendline(password)
child.expect('#')
#we should now be inside the router, time to find out what version it is running
print "Trying to detect router version"
child.sendline('show version')
status = child.expect(['#','--More--'])
tmp_holder = child.before <------- succesfully retrieving the router version information.
if status == 1:
child.sendline('q')
child.expect('#')
if "7200 Software" in tmp_holder:
print "---Router Version CISCO 7200 Detected---"
print "---Checking BGP status---"
for neighbor in host.getbgpip():
child.sendline('show bgp all neighbors %s ' % (neighbor))
bgp_status = child.expect(['#', '--More--'])
bgp_status_holder = child.before # <---this part is failing, not getting the return from show bgp all neigbors
if bgp_status == 1:
child.sendline('q')
child.expect('#')
if 'Established' in bgp_status_holder:
print host.gethostname()+" established to "+neighbor
elif 'Idle' in bgp_status_holder or "Active" in bgp_status_holder:
print host.gethostname()+" not established to "+neighbor
else:
print host.gethostname()+" non existing neighbor: "+neighbor
print bgp_status_holder
#child.interact() <---if i activate this i get the result printed, plus i'm seeing the "--More--" that i'm trying to catch above.
child.close()
如果我在尝试设置bgp_status_holder变量时使用child.after而不是child.before我只得到#作为结果。