python.strip()一次一行

python.strip()一次一行,python,pexpect,Python,Pexpect,您好,我正在尝试从外部文件向Cisco/Juniper路由器发送多个show命令。我有一个要求做这件事。。。但出于某种原因,我一直有一个问题,即立即发送所有内容。我正试图用pxpect来实现这一点。如果我有10个显示命令,我想运行,我只有2或3。。。有什么帮助吗?这就是Python import sys,pexpect,getpass,time username = raw_input("Username: ") password = getpass.getpass() routerFile

您好,我正在尝试从外部文件向Cisco/Juniper路由器发送多个show命令。我有一个要求做这件事。。。但出于某种原因,我一直有一个问题,即立即发送所有内容。我正试图用pxpect来实现这一点。如果我有10个显示命令,我想运行,我只有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

作为旁注:仅仅调用command.strip不起任何作用;这将返回命令的精简版本,您可以忽略它。但在下一行中,您再次调用它并使用返回值。child是pexpect.spawn还是类似的?如果是这样,为什么这是标记的网络编程而不是标记的pexpect?如果是因为您希望使用ssh命令,那么您使用pexpect这一事实对于您的问题可能比您使用它在某种程度上与网络上的某个对象进行对话这一事实更为重要。如果没有,是什么?最后,实际的反应是什么样的?你收到10个提示了吗?如果没有,在第二或第三条发送线之后,您得到了什么?为什么您要两次调用strip并忽略第一个结果?