使用telnet的python-pexpect引发EOF异常

时间:2018-02-22 23:57:00

标签: python pexpect

我有一个脚本尝试使用telnet连接到终端服务器并将portnumber作为参数传递给访问控制台。使用Python CLI做同样的事情我有很大的成功。但是这个成功并没有用脚本复制。以下是cli的输出:

>>> import pexpect
>>> console = pexpect.spawn('/usr/bin/telnet k2dc-term1-kk2 6026')
>>> console
<pexpect.spawn object at 0x7fdc70dd3450>
>>> console.sendline('\n')
2
>>>>>> console.expect(['LOADER-A\*>'])
0

现在,谈到脚本:

import pexpect
import sys

console = pexpect.spawn('/usr/bin/telnet', ['k2dc-term1-kk7 6026'])

index = console.expect(['LOADER-A*>'])

我得到的输出是:

>./texpect.py
Traceback (most recent call last):
  File "./texpect.py", line 8, in <module>
    index = console.expect(['LOADER-A*>'])
  File "/usr/software/lib/python2.7/site-packages/pexpect/__init__.py", line 1354, in expect
    return self.expect_list(compiled_pattern_list, timeout, searchwindowsize)
  File "/usr/software/lib/python2.7/site-packages/pexpect/__init__.py", line 1368, in expect_list
    return self.expect_loop(searcher_re(pattern_list), timeout, searchwindowsize)
  File "/usr/software/lib/python2.7/site-packages/pexpect/__init__.py", line 1439, in expect_loop
    raise EOF (str(e) + '\n' + str(self))
pexpect.EOF: End Of File (EOF) in read_nonblocking(). Exception style platform.
<pexpect.spawn object at 0x7fd281666310>
version: 2.5.1
command: /usr/bin/telnet
args: ['/usr/bin/telnet', 'k2dc-term1-kk7 6026']
searcher: searcher_re:
    0: re.compile("LOADER-A*>")
buffer (last 100 chars):
before (last 100 chars): telnet: k2dc-term1-kk7 6026: Name or service not known

after: <class 'pexpect.EOF'>
match: None
match_index: None
exitstatus: 1
flag_eof: True
pid: 21520
child_fd: 3
closed: False
timeout: 30
delimiter: <class 'pexpect.EOF'>
logfile: None
logfile_read: None
logfile_send: None
maxread: 2000
ignorecase: False
searchwindowsize: None
delaybeforesend: 0.05
delayafterclose: 0.1
delayafterterminate: 0.1

另外我注意到,如果我将终端服务器和端口号传递到一个单独的列表中,pexpect会首先尝试连接到终端服务器。但我想知道Python CLI和脚本之间的区别。如果有人能够对此有所了解,非常感谢。

0 个答案:

没有答案