简体   繁体   中英

Python PEXPECT - TIMEOUT

I was just trying to set up a script to retrieve some outputs from a unix-based device, here you can find my code:

import pexpect
import time,sys, traceback, os



telconn = pexpect.spawn('telnet 192.168.1.1')
telconn.logfile = sys.stdout

telconn.expect("Enter Choice>")
telconn.send("tacl" + "\r")

telconn.expect("TACL 1>")
telconn.send("logon node.mgr" + "\r")


telconn.expect("Password:")
telconn.send("psw" + "\r")

telconn.expect('$SYSTEM NODEMGR 1>')
telconn.send("osh" + "\r") 

Actually it works fine until it retrieves the banner (I don't if it could be the issue) and the right next cli prompt, here you can have a look at the output:

python try.py 
Trying 192.168.1.1...
Connected to 192.168.1.1.
Escape character is '^]'.




Available Services:

TACL     EXIT     
taclr Choice> tacl
logon node.mgrnode.mgr
Password: psw
Last Logon:  03 NOV 2015, 15:18
Last Unsuccessful Attempt: 03 NOV 2015, 15:17  Total Failures: 24
********************************************************************************November 3, 2015 15:51:57
********************************************************************************
              This is a private system operated for XXX XXXXX

            Authorization from XXX XXXXX management is required
                           for access to this system

                   Use by unauthorized persons is prohibited


********************************************************************************
********************************************************************************(Invoking $SYSTEM.NODEMGR.TACLCSTM)

Loaded from $SYSTEM.CBCLOCL.TACLMACS:

FI FN V SV P F H DS CD LS CAT QUEUE FS PE SPE SP T SCPA SCPB SCPC SCPD

Current volume is $SYSTEM.NODEMGR


 $SYSTEM NODEMGR 1> Traceback (most recent call last):

  File "try.py", line 22, in <module>
    telconn.expect('$SYSTEM NODEMGR 1>')
  File "/usr/lib/python2.7/dist-packages/pexpect/__init__.py", line 1418, in expect
    timeout, searchwindowsize)
  File "/usr/lib/python2.7/dist-packages/pexpect/__init__.py", line 1433, in expect_list
    timeout, searchwindowsize)
  File "/usr/lib/python2.7/dist-packages/pexpect/__init__.py", line 1535, in expect_loop
    raise TIMEOUT(str(err) + '\n' + str(self))
pexpect.TIMEOUT: Timeout exceeded.
<pexpect.spawn object at 0xb7259acc>
version: 3.1
command: /usr/bin/telnet
args: ['/usr/bin/telnet', '192.168.1.1']
searcher: <pexpect.searcher_re object at 0xb726f7ac>
buffer (last 100 chars): 'T QUEUE FS PE SPE SP T SCPA SCPB SCPC SCPD\r\n\r\nCurrent volume is $SYSTEM.NODEMGR\r\n$SYSTEM NODEMGR 1> '
before (last 100 chars): 'T QUEUE FS PE SPE SP T SCPA SCPB SCPC SCPD\r\n\r\nCurrent volume is $SYSTEM.NODEMGR\r\n$SYSTEM NODEMGR 1> '
after: <class 'pexpect.TIMEOUT'>
match: None
match_index: None
exitstatus: None
flag_eof: False
pid: 10135
child_fd: 3
closed: False
timeout: 30
delimiter: <class 'pexpect.EOF'>
logfile: <open file '<stdout>', mode 'w' at 0xb753e078>
logfile_read: None
logfile_send: None
maxread: 2000
ignorecase: False
searchwindowsize: None
delaybeforesend: 0.05
delayafterclose: 0.1
delayafterterminate: 0.1

As you can see, the last prompt I have is:

$SYSTEM NODEMGR 1> 

After that I got the: Traceback (most recent call last):

Anyone can help me in fix that issue? I would like just to go ahead in retrieving some output, but I'm stuck at this point!

The $ char in telconn.expect('$SYSTEM NODEMGR 1>') is interpreted as a regex metacharacter, which, according to the doc :

Matches at the end of a line, which is defined as either the end of the string, or any location followed by a newline character.

This causes your script to fail matching the input, which contains a literal $ char, leading to the timeout.

You have a few options:

  • escape the regex metacharacter, with care
  • use .expect_exact() instead of .expect() , which uses plain string matching instead of compiled regular expressions
  • reduce the pattern matching by dropping the $ altogether (ie use telconn.expect('SYSTEM NODEMGR 1>') ), if the remaining string is still significant enough for proper script operation

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