我正熟悉pexpect。我编写了下面的代码片段来解开cisco路由器的端口通道。直到第89行,当我 看到stdout输出,没有问题。
以下是代码段:
54 deviceEnable = data[0] + ">"
55 deviceExec = data[0] + "#"
56 deviceConfig = data[0] + "(config)#"
57 deviceIfConfig = data[0] + "(config-if)#"
58 k = device.expect([deviceEnable, deviceExec, deviceConfig])
59 if k == 0:
60 device.sendcontrol('c')
61 device.expect(deviceEnable)
62 device.sendline('enable')
63 device.expect('Password:')
64 device.sendline(data[4])
65 elif k == 1:
66 device.sendcontrol('c')
67 elif k == 2:
68 device.sendcontrol('c')
69 device.expect(deviceConfig)
70 device.sendline('end')
71 ###################################
72 # Uplink Unflap SOP
73 ###################################
74 device.logfile = sys.stdout
75 device.expect(deviceExec)
76 device.sendline('show int status | in Po')
77 device.expect(deviceExec)
78 pcStatus1 = device.before
79 temp1 = pcStatus1.split('\n')
80 temp2 = temp1[2]
81 pcStatus = temp2.split()
82 print("\n %s \n" % (pcStatus))
83 match1 = re.match('Err-Disable', pcStatus[1])
84 # match1 = re.match('notconnect', pcStatus[2])
85 if match1:
86 print("Entered here \n")
87 # device.expect(deviceExec)
88 print("Task 0 complete")
89 device.sendline('conf t')
90 device.expect(deviceConfig)
91 print("Task 1 complete")
然而,对于第89行,“conf t”会被发送两次。见下文:
<device-name>#show int status | in Po
show int status | in Po
Port Name Status Vlan Duplex Speed Type
Po1 Err-Disable notconnect routed auto auto
<device-name>#
['Po1', 'Err-Disable', 'notconnect', 'routed', 'auto', 'auto']
Entered here
conf t
conf t
Enter configuration commands, one per line. End with CNTL/Z.
<device-name>(config)#Exception in thread Thread-1:
Traceback (most recent call last):
File "/usr/local/lib/python2.7/threading.py", line 530, in __bootstrap_inner
self.run()
File "/home/nseshan/unflapper/ThreadPool.py", line 202, in run
cmd(args)
File "/home/nseshan/unflapper/deviceLogin.py", line 91, in devLogin
device.expect(deviceConfigEntry)
File "/usr/local/lib/python2.7/site-packages/pexpect/__init__.py", line 1451, in expect
timeout, searchwindowsize)
File "/usr/local/lib/python2.7/site-packages/pexpect/__init__.py", line 1466, in expect_list
timeout, searchwindowsize)
File "/usr/local/lib/python2.7/site-packages/pexpect/__init__.py", line 1568, in expect_loop
raise TIMEOUT(str(err) + '\n' + str(self))
TIMEOUT: Timeout exceeded.
<pexpect.spawn object at 0x86b162c>
version: 3.3
command: /usr/bin/ssh
args: ['/usr/bin/ssh', 'sjc17-1-tea005']
searcher: <pexpect.searcher_re object at 0x86b19ac>
buffer (last 100 chars): 'conf t\r\nEnter configuration commands, one per line. End with CNTL/Z.\r\nsjc17-1-tea005(config)#'
before (last 100 chars): 'conf t\r\nEnter configuration commands, one per line. End with CNTL/Z.\r\nsjc17-1-tea005(config)#'
after: <class 'pexpect.TIMEOUT'>
match: None
match_index: None
exitstatus: None
flag_eof: False
pid: 8182
child_fd: 4
closed: False
timeout: 30
delimiter: <class 'pexpect.EOF'>
logfile: <open file '<stdout>', mode 'w' at 0x8232078>
logfile_read: None
logfile_send: None
maxread: 2000
ignorecase: False
searchwindowsize: None
delaybeforesend: 0.05
delayafterclose: 0.1
delayafterterminate: 0.1
请注意,缓冲区现在包含不需要的字符以及我期望的提示。当我尝试在“conf t”提示符中使用.sendline()发出下一个命令时,这会导致pexpect的超时。
我不确定接下来要做什么,因为我一整天都试图解决这个问题,并且没有随时随地解决这个问题。有什么建议吗?
答案 0 :(得分:1)
使用.logfile_read=sys.stdout
代替.logfile=sys.stdout
,to log only what the child sends back。