使用 pexpect 和 pxssh 时的 EOF [英] EOF when using pexpect and pxssh

查看:107
本文介绍了使用 pexpect 和 pxssh 时的 EOF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 暴力蟒蛇.同时使用 child.expect()pxssh 我得到了类似的 EOF 错误.

I'm trying to run the code in the Interacting with SSH Through Pexpect and Brute Forcing SSH Passwords with Pxssh sections from Chapter 2 of Violent Python. Using both child.expect() and pxssh I get similar EOF errors.

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

Running these commands from the Python console:

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 运行这些命令:

And running these commands, using 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 并尝试不同的用户名/密码组合时,我得到了类似的结果.

I get similar results when I substitute 127.0.0.1 with other hosts and try different username/password combinations.

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

The pexpect documentation suggests using expect(pexpect.EOF) to avoid generating the EOF exception. Indeed, when I do the following:

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 吗?
  1. I'm confused by the book's syntax: child.expect([pexpect.TIMEOUT, ssh_newkey, "[P|p]assword:"]). Why are we passing a list to expect()? What is this list supposed to contain?
  2. How do I make use of expect(pexpect.EOF), as the documentation instructs, when using pxssh?
  3. Why does the code in the book not work properly? Has something changed in the pexpect library since the book's publication? Is it because I am on OS X?

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

I have Python 2.7 and pexpect 2.4 running on Mac OS X 10.8.4.

推荐答案

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

Regarding #2: Expecting EOF is a red herring here. You -don't- expect EOF on login, you expect a password prompt on login. pxssh kicks that error when it gets back EOF on login from ssh without getting a password prompt. This can happen because it's using ssh -q to not get warnings, and you're getting a warning from ssh. Take the ssh options it is using and run them yourself without the q:

/usr/bin/ssh -l root 127.0.0.1

/usr/bin/ssh -l root 127.0.0.1

就我而言,当 ssh 由于我连接到的机器的身份已更改而踢出已知的主机违规时,我会收到此错误消息.

In my case, I can get this error message when ssh is kicking out a known hosts violation due to the machine I am connecting to having its identity changed.

这篇关于使用 pexpect 和 pxssh 时的 EOF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆