【问题标题】:EOF when using pexpect and pxssh使用 pexpect 和 pxssh 时的 EOF
【发布时间】:2013-07-26 10:57:03
【问题描述】:

我正在尝试运行Violent Python 第 2 章中通过 Pexpect 与 SSH 交互使用 Pxssh 强制使用 SSH 密码部分中的代码。同时使用 child.expect()pxssh 我得到类似的 EOF 错误。

从 Python 控制台运行这些命令:

import pexpect
connStr = "ssh root@127.0.0.1"
child = pexpect.spawn(connStr)
ret = child.expect([pexpect.TIMEOUT, ssh_newkey, "[P|p]assword:"])

我得到这个输出:

Traceback (most recent call last):
  File "<input>", line 1, in <module>
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pexpect.py", li
ne 1316, in expect
    return self.expect_list(compiled_pattern_list, timeout, searchwindowsize)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pexpect.py", li
ne 1330, in expect_list
    return self.expect_loop(searcher_re(pattern_list), timeout, searchwindowsize)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pexpect.py", li
ne 1401, in expect_loop
    raise EOF (str(e) + '\n' + str(self))
EOF: End Of File (EOF) in read_nonblocking(). Empty string style platform.
<pexpect.spawn object at 0x10180c550>
version: 2.4 ($Revision: 516 $)
command: /usr/bin/ssh
args: ['/usr/bin/ssh', 'root@127.0.0.1']
searcher: searcher_re:
    0: TIMEOUT
    1: re.compile("Are you sure you want to continue connecting")
    2: re.compile("[P|p]assword:")
buffer (last 100 chars): 
before (last 100 chars): 
after: <class 'pexpect.EOF'>
match: None
match_index: None
exitstatus: 255 
flag_eof: True
pid: 12122
child_fd: 4
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

并运行这些命令,使用pxssh:

import pxssh
s = pxssh.pxssh()
s.login("127.0.0.1", "root", "1234")

我得到这个输出:

Traceback (most recent call last):
  File "<input>", line 1, in <module>
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pxssh.py", line
 196, in login
    i = self.expect(["(?i)are you sure you want to continue connecting", original_prompt, "(?i)(?:pas
sword)|(?:passphrase for key)", "(?i)permission denied", "(?i)terminal type", TIMEOUT, "(?i)connectio
n closed by remote host"], timeout=login_timeout)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pexpect.py", li
ne 1316, in expect
    return self.expect_list(compiled_pattern_list, timeout, searchwindowsize)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pexpect.py", li
ne 1330, in expect_list
    return self.expect_loop(searcher_re(pattern_list), timeout, searchwindowsize)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pexpect.py", li
ne 1401, in expect_loop
    raise EOF (str(e) + '\n' + str(self))
EOF: End Of File (EOF) in read_nonblocking(). Empty string style platform.
<pxssh.pxssh object at 0x1016bff90>
version: 2.4 ($Revision: 516 $)
command: /usr/bin/ssh
args: ['/usr/bin/ssh', '-q', '-l', 'root', '127.0.0.1']
searcher: searcher_re:
    0: re.compile("(?i)are you sure you want to continue connecting")
    1: re.compile("[#$]")
    2: re.compile("(?i)(?:password)|(?:passphrase for key)")
    3: re.compile("(?i)permission denied")
    4: re.compile("(?i)terminal type")
    5: TIMEOUT
    6: re.compile("(?i)connection closed by remote host")
buffer (last 100 chars): 
before (last 100 chars): 
after: <class 'pexpect.EOF'>
match: None
match_index: None
exitstatus: None
flag_eof: True
pid: 12136
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

当我将 127.0.0.1 替换为其他主机并尝试不同的用户名/密码组合时,我得到了类似的结果。

pexpect documentation 建议使用expect(pexpect.EOF) 来避免生成 EOF 异常。确实,当我执行以下操作时:

connStr = "ssh root@127.0.0.1"
child = pexpect.spawn(connStr)
print child.expect(pexpect.EOF)

结果是0

但以下问题仍然存在:

  1. 我对这本书的语法感到困惑:child.expect([pexpect.TIMEOUT, ssh_newkey, "[P|p]assword:"])。为什么我们将列表传递给expect()?这个列表应该包含什么?
  2. 在使用 pxssh 时,如何按照文档说明使用 expect(pexpect.EOF)
  3. 为什么书中的代码不能正常工作?自这本书出版以来,pexpect 图书馆有什么变化吗?是因为我在 OS X 上吗?

我在 Mac OS X 10.8.4 上运行 Python 2.7 和 pexpect 2.4。

【问题讨论】:

  • 您可能添加了尝试连接主机的计算机的 ssh 公钥。您确定在尝试连接时会收到密码提示吗?
  • 我也面临同样的问题。有解决办法吗?

标签: python python-2.7 pexpect


【解决方案1】:

关于#2:期待 EOF 在这里是一个红鲱鱼。您-不希望-登录时出现EOF,您希望登录时出现密码提示。 pxssh 在从 ssh 登录时返回 EOF 而没有收到密码提示时会触发该错误。发生这种情况是因为它使用 ssh -q 没有收到警告,而您收到了来自 ssh 的警告。使用它正在使用的 ssh 选项并在没有 q 的情况下自行运行它们:

/usr/bin/ssh -l root 127.0.0.1

在我的情况下,当 ssh 由于我连接到的机器的身份已更改而排除已知主机违规时,我会收到此错误消息。

【讨论】:

    【解决方案2】:

    我在尝试在 pxssh 中使用 self.expect() 时遇到了同样的错误。通过删除“-q”来更改 ssh 选项对我不起作用。

    我按照this 站点中的说明将 pexpect.EOF 添加为self.expect() 的输入之一。这将使脚本在收到 EOF 时退出,直到收到整个字符串:

    i = self.expect(["(?i)are you sure you want to continue connecting", original_prompt, "(?i)(?:password.*)|(?:passphrase for key)", "(?i)permission denied", "(?i)terminal type", pexpect.EOF, TIMEOUT, "(?i)connection closed by remote host"], timeout=login_timeout)
    

    有了这个,效果很好!

    【讨论】:

      【解决方案3】:

      我知道这个问题已经很久没有提出来了,但我花了很长时间才弄清楚,也许我可以帮助下一个遇到这个问题的人。

      我发现问题是由超时引起的。方法login() 有一个默认值为login_timeout=10 的参数。如果您将其设置为 20 而不是 10,那么书中的原始脚本可以正常工作。结果行是:

      s.login(host, user, password, login_timeout=20)
      

      也许接下来我应该尝试找出为什么我的 ssh 登录速度这么慢...

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-12-22
        • 2019-02-26
        • 2016-08-20
        • 1970-01-01
        • 2017-06-23
        • 1970-01-01
        • 2017-04-17
        • 2023-03-16
        相关资源
        最近更新 更多