微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

升级到 Python3 后,PXSSH 连接有时会随机失败

如何解决升级到 Python3 后,PXSSH 连接有时会随机失败

我正在尝试使用 pexpect.pxssh 创建一个 ssh 会话,如下所示:

from pexpect import pxssh
connection = pxssh.pxssh()
connection.login('localhost',username,password,port=port,check_local_ip=False)
"""
Fails with the following error
pexpect.pxssh.ExceptionPxssh: Could not establish connection to host
"""

我还一个一个地创建了两个会话,第一个会话连接没有问题,但第二个会话无法使用相同的代码连接。此外,有时代码可以正常工作并且能够两次连接。我还添加了重试,以确保它不是随机事件。

另一件需要注意的事情是,这段代码在 Python 2 中运行没有问题,但在 Python 3 中会发生这种情况。我在 b/w Python2 和 python3 的连接机制中找不到任何区别。任何帮助将不胜感激!

编辑:根据评论添加日志后:

2021-06-25 10:49:37 信息 - 尝试连接到端口 10022 上的设备。
正在连接 USB 设备...
6 月 25 日 10:49:37 tcprelay[203]:创建线程以将 [::1]:10022->[::1]:58316 连接到 unix:0
user@localhost 的密码:xxxxx
6 月 25 日 10:49:37 tcprelay[203]:退出线程以将 [::1]:10022->[::1]:58316 连接到 unix:0

正在连接到 USB 设备...
6 月 25 日 10:49:38 tcprelay[203]:创建线程以将 [::1]:10022->[::1]:58317 连接到 unix:0
user@localhost 的密码:xxxxx
6 月 25 日 10:49:39 tcprelay[203]:退出线程以将 [::1]:10022->[::1]:58316 连接到 unix:0

代码重试 2 次,然后失败。 注意:我正在使用 tcprelay

添加一个 10000 的端口偏移量

编辑:

抱歉,我没有正确记录错误

2021-06-25 15:45:34 - ERROR - Failed to connect. retrying...
2021-06-25 15:45:34 - ERROR - End Of File (EOF). Empty string style platform.
<pexpect.pxssh.pxssh object at 0x127feb0a0>
command: /usr/bin/ssh
args: ['/usr/bin/ssh','-q','-oNoHostAuthenticationForLocalhost=yes','-p','xxxxx','-l','localhost']
buffer (last 100 chars): b''
before (last 100 chars): b' \r\n'
after: <class 'pexpect.exceptions.EOF'>
match: None
match_index: None
exitstatus: None
flag_eof: True
pid: 30020
child_fd: 26
closed: False
timeout: 60
delimiter: <class 'pexpect.exceptions.EOF'>
logfile: <_io.BufferedWriter name='<stdout>'>
logfile_read: None
logfile_send: None
maxread: 2000
ignorecase: False
searchwindowsize: None
delaybeforesend: 0.05
delayafterclose: 0.1
delayafterterminate: 0.1
searcher: searcher_re:
    0: re.compile(b'(?i)are you sure you want to continue connecting')
    1: re.compile(b'[#$]')
    2: re.compile(b'(?i)(?:password:)|(?:passphrase for key)')
    3: re.compile(b'(?i)permission denied')
    4: re.compile(b'(?i)terminal type')
    5: TIMEOUT
Traceback (most recent call last):
  File "/src/helpers/utilities.py",line 590,in try_connect_ssh
    connection.make_connection(ipaddress=ipaddress,user=user,File "/src/transport/myssh.py",line 26,in make_connection
    self.ssh_process.login(ipaddress,user,sync_multiplier=5,check_local_ip=False)
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/pexpect/pxssh.py",line 418,in login
    i = self.expect(session_regex_array)
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/pexpect/spawnbase.py",line 343,in expect
    return self.expect_list(compiled_pattern_list,File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/pexpect/spawnbase.py",line 372,in expect_list
    return exp.expect_loop(timeout)
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/pexpect/expect.py",line 179,in expect_loop
    return self.eof(e)
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/pexpect/expect.py",line 122,in eof
    raise exc
pexpect.exceptions.EOF: End Of File (EOF). Empty string style platform.
<pexpect.pxssh.pxssh object at 0x127feb0a0>

编辑:

  • 使用 macOS Mojave
  • pExpect 3.8.0
  • 设备要求输入密码,但发送密码后连接返回 EOF

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。