Python 预期 - 超时

Python PEXPECT - TIMEOUT

我只是想设置一个脚本来从基于 unix 的设备中检索一些输出,在这里你可以找到我的代码:

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

实际上它工作正常,直到它检索横幅(如果它可能是问题,我不知道)和右下一个 cli 提示,在这里你可以看看输出:

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

如您所见,我得到的最后一个提示是:

$SYSTEM NODEMGR 1> 

之后我得到了:Traceback (most recent call last):

谁能帮我解决这个问题? 我只想继续检索一些输出,但我卡在了这一点上!

telconn.expect('$SYSTEM NODEMGR 1>') 中的 $ 字符被解释为正则表达式元字符,根据 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.

这会导致您的脚本无法匹配包含文字 $ 字符的输入,从而导致超时。

您有几个选择:

  • 转义正则表达式元字符,with care
  • 使用.expect_exact()而不是.expect(),它使用纯字符串匹配而不是编译的正则表达式
  • 通过完全删除 $ 来减少模式匹配(即使用 telconn.expect('SYSTEM NODEMGR 1>')),如果剩余的字符串对于正确的脚本操作仍然足够重要