python pexpect pxssh scp_python – 使用pexpect和pxssh时的EOF

本文档描述了在使用Python的pexpect和pxssh模块进行SSH连接时遇到的EOF错误问题。作者展示了使用这两个库尝试连接到127.0.0.1的代码片段,并给出了遇到的错误堆栈。尽管尝试使用pexpect.EOF来避免EOF异常,但问题依然存在。文章探讨了代码语法、列表参数含义、pxssh文档中的建议以及可能的平台相关性问题。
摘要由CSDN通过智能技术生成

我正在尝试使用07x第2章中的Pxssh部分通过Pexpect和Brute强制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 "", line 1, in

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.

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:

match: None

match_index: None

exitstatus: 255

flag_eof: True

pid: 12122

child_fd: 4

closed: False

timeout: 30

delimiter:

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 "", line 1, in

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.

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:

match: None

match_index: None

exitstatus: None

flag_eof: True

pid: 12136

child_fd: 3

closed: False

timeout: 30

delimiter:

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.

但仍存在以下问题:

>我对这本书的语法感到困惑:child.expect([pexpect.TIMEOUT,ssh_newkey,“[P | p] assword:”]).为什么我们将列表传递给expect()?该列表应该包含什么?

>当使用pxssh时,如何在文档中指示使用expect(pexpect.EOF)?

>为什么书中的代码不能正常工作?自该书出版以来,pexpect库中有什么变化吗?是因为我在OS X上吗?

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值