pexpect python throw错误

时间:2016-05-06 18:10:28

标签: python-3.4 pexpect

虽然这是我第一次尝试使用pexpect,但使用pexpect的python3脚本非常简单;但它失败了。

#!/usr/bin/env python3
import sys
import pexpect

SSH_NEWKEY = r'Are you sure you want to continue connecting \(yes/no\)\?'

child = pexpect.spawn("ssh -i /user/aws/key.pem ec2-user@xxx.xxx.xxx.xxx date")
i = child.expect( [ pexpect.TIMEOUT, SSH_NEWKEY )
if i == 1:
   child.sendline('yes')
print(child.before)

SSH_NEWKEY是我期待的唯一响应,但是示例显示了一个包含pexpect.TIMEOUT的列表,因此我使用了它。

$ ./test.py 
Traceback (most recent call last):
  File "/usr/local/lib/python3.4/site-packages/pexpect/spawnbase.py", line 144, in read_nonblocking
    s = os.read(self.child_fd, size)
OSError: [Errno 5] Input/output error

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/local/lib/python3.4/site-packages/pexpect/expect.py", line 97, in expect_loop
    incoming = spawn.read_nonblocking(spawn.maxread, timeout)
  File "/usr/local/lib/python3.4/site-packages/pexpect/pty_spawn.py", line 455, in read_nonblocking
    return super(spawn, self).read_nonblocking(size)
  File "/usr/local/lib/python3.4/site-packages/pexpect/spawnbase.py", line 149, in read_nonblocking
    raise EOF('End Of File (EOF). Exception style platform.')
pexpect.exceptions.EOF: End Of File (EOF). Exception style platform.

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "./min.py", line 15, in <module>
    i = child.expect( [ pexpect.TIMEOUT, SSH_NEWKEY  ] )
  File "/usr/local/lib/python3.4/site-packages/pexpect/spawnbase.py", line 315, in expect
    timeout, searchwindowsize, async)
  File "/usr/local/lib/python3.4/site-packages/pexpect/spawnbase.py", line 339, in expect_list
    return exp.expect_loop(timeout)
  File "/usr/local/lib/python3.4/site-packages/pexpect/expect.py", line 102, in expect_loop
    return self.eof(e)
  File "/usr/local/lib/python3.4/site-packages/pexpect/expect.py", line 49, in eof
    raise EOF(msg)
pexpect.exceptions.EOF: End Of File (EOF). Exception style platform.
<pexpect.pty_spawn.spawn object at 0x7f70ea4fbcf8>
command: /usr/bin/ssh
args: ['/usr/bin/ssh', '-i', '/user/aws/key.pem', 'ec2-user@xxx.xxx.xxx.xxx', 'date']
searcher: None
buffer (last 100 chars): b''
before (last 100 chars): b'Fri May  6 13:50:18 EDT 2016\r\n'
after: <class 'pexpect.exceptions.EOF'>
match: None
match_index: None
exitstatus: 0
flag_eof: True
pid: 31293
child_fd: 5
closed: False
timeout: 30
delimiter: <class 'pexpect.exceptions.EOF'>
logfile: None
logfile_read: None
logfile_send: None
maxread: 2000
ignorecase: False
searchwindowsize: None
delaybeforesend: 0.05
delayafterclose: 0.1
delayafterterminate: 0.1

我错过了什么?

CentOS 6.4 python 3.4.3

1 个答案:

答案 0 :(得分:0)

期待通话期间会出现EOF错误。这意味着收到的响应与SSH_NEWKEY不匹配,并在超时期限内到达文件末尾。要捕获此异常,您应将except行更改为:

i = child.expect( [ pexpect.TIMEOUT, SSH_NEWKEY, pexpect.EOF)

然后你可以让你更健壮:

if i == 1:
   child.sendline('yes')
elif i == 0:
    print "Timeout"
elif i == 2:
    print "EOF"
print(child.before)

这并不能解决为什么你收到带有预期字符串的响应的原因 - 如果不查看更多代码就很难知道,但很可能是因为你的响应有点错误。如果手动输入SSH字符串,您应该能够看到预期的响应,并在代码中输入此响应。

您也可以在预期电话结束后打印child.before,或打印您的预期电话的child.read() ,以查看作为回复发送的内容。