您好我正在尝试从外部文件向Cisco / Juniper路由器发送多个show命令。我有要求这样做...但由于某种原因,我一直有一个发送所有内容的问题。我试图用pxpect做这个。如果我想要运行10个show命令,我只能获得2或3 ...任何帮助?这是Python。
import sys,pexpect,getpass,time
username = raw_input("Username: ")
password = getpass.getpass()
routerFile = open('devicelist', 'r')
routers = [i for i in routerFile]
for router in routers:
try:
child = pexpect.spawn ('telnet', [router.strip()])
child.expect(['[uU]sername:', '[lL]ogin:'], timeout=3)
child.sendline(username)
child.expect('[pP]assword:')
child.sendline(password)
software = child.expect([ '#'], 6)
print ("Logging into " + router.strip())
except:
print "\nCould not log in to " + router.strip()
pass
commandlist = 'junos'
commands = open(commandlist, 'r')
for command in commands:
command.strip()
child.sendline(command.strip())
child.expect(['>'],20)
print child.before
这些是我试图发送的命令:
show interface terse | no-more | match lo
show interfaces descriptions
show interfaces descriptions
show interfaces lo0.0 | match local
show route | match mpls
show system uptime | match boot
这是输出..从未完成......
Username: script
Password:
--- JUNOS 12.1R1.9 built 2012-03-24 12:52:33 UTC
script@Junos-R1
show interfaces terse | no-more | match lo
Interface Admin Link Proto Local Remote
lo0 up up
lo0.0 up up inet 12.12.12.12 --
0/0
lo0.16384 up up inet 127.0.0.1 --
0/0
lo0.16385 up up inet 128.0.0.4 --
0/0
script@Junos-R1
show interfaces descriptions
Interface Admin Link Description
em1 down down TEST
em3 up down TO IOS-XRv-1
em6 up down TO JUNOS-R2
lo0.0 up up Test of the script
script@Junos-R1
show interfaces descriptions
Interface Admin Link Description
em1 down down TEST
em3 up down TO IOS-XRv-1
em6 up down TO JUNOS-R2
lo0.0 up up Test of the script
script@Junos-R1
show interfaces terse