简体   繁体   中英

pexpect echoes sendline output twice causing unwanted characters in buffer

I am getting myself familiarized with pexpect. I have written the below snippet of code to unflap a port-channel of a cisco router. Up until line 89, when I see the stdout outputs, there is no problem.

Below is the code snippet:

 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")

However, for line 89, "conf t" gets sent twice. See below:

<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

Notice that the buffer now holds unwanted characters as well as the prompt that I am expecting. This causes a timeout from pexpect's side when I try to issue the next command using .sendline() in the "conf t" prompt.

I am unsure what to do next as I have been trying to fix this all day and havent gotten anywhere with it. Any suggestions?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM