pexpect两次回显发送行输出,导致缓冲区中出现不需要的字符



我正在熟悉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行,"conft"会被发送两次。见下文:

<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 trnEnter configuration commands, one per line.  End with CNTL/Z.rnsjc17-1-tea005(config)#'
before (last 100 chars): 'conf trnEnter configuration commands, one per line.  End with CNTL/Z.rnsjc17-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

请注意,缓冲区现在包含了不需要的字符以及我期望的提示。当我尝试在"conft"提示符中使用.sedline()发出下一个命令时,这会导致pexpect方面的超时。

我不确定下一步该怎么办,因为我一整天都在努力解决这个问题,但没有取得任何进展。有什么建议吗?

使用.logfile_read=sys.stdout而不是.logfile=sys.stdout,只记录子级发回的内容。

最新更新