gpt4 book ai didi

python - 使用 pxssh 进行 SSH 时 python 代码出错

转载 作者:太空宇宙 更新时间:2023-11-04 12:35:04 24 4
gpt4 key购买 nike

我正在尝试通过 python 连接 SSH。我尝试使用不同的方法来消除错误,但仍在努力消除错误。这是我的代码:

1) import pxssh
import getpass
def get_ssh():
s= pxssh.pxssh()
hostname = raw_input('IP: ')
username = raw_input('username: ')
password = getpass.getpass('password: ')
s.login (IP, username, password)
s.logout()
return True

2) s= pxssh.pxssh(timeout='time_out', maxread=20000)
s.SSH_OPTS += " -o StrictHostKeyChecking=no"
s.SSH_OPTS += " -o UserKnownHostsFile=/dev/null"
s.login ('IP', 'username', 'password')

在这两种方式中,我都遇到了同样的错误。我还需要一些有关如何传递私钥和公钥进行身份验证的信息:

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "mit_test.py", line 11, in get_ssh
s.login (hostname, username, password)
File "/usr/lib/python2.7/dist-packages/pxssh.py", line 196, in login
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",
TIMEOUT, "(?i)connection closed by remote host"], timeout=login_timeout)
File "/usr/lib/python2.7/dist-packages/pexpect.py", line 1316, in expect
return self.expect_list(compiled_pattern_list, timeout, searchwindowsize)
File "/usr/lib/python2.7/dist-packages/pexpect.py", line 1330, in expect_list
return self.expect_loop(searcher_re(pattern_list), timeout, searchwindowsize)
File "/usr/lib/python2.7/dist-packages/pexpect.py", line 1401, in expect_loop
raise EOF (str(e) + '\n' + str(self))
pexpect.EOF: End Of File (EOF) in read_nonblocking(). Exception style platform.
<pxssh.pxssh object at 0x2840ad0>
version: 2.4 ($Revision: 516 $)
command: /usr/bin/ssh
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")

我到底需要做什么?我搜索了一些其他评论,但找不到任何解决方案。另外,如何在登录参数中设置私钥路径?谁能帮我?我浏览了不同的帖子 link [链接] ( EOF when using pexpect and pxssh ) 但找不到解决方案。谢谢你的时间。

最佳答案

您的程序尝试连接的主机是新的主机。首先必须接受主机的 key ,然后才能进行身份验证。在终端上,这需要键入"is"。您可以使用 SSH 选项关闭主机 key 检查。

s = pxssh.pxssh(options={
"StrictHostKeyChecking": "no"})

关于python - 使用 pxssh 进行 SSH 时 python 代码出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42155873/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com