gpt4 book ai didi

python - paramiko.SSHException : Channel closed 异常

转载 作者:行者123 更新时间:2023-11-28 18:45:38 33 4
gpt4 key购买 nike

我尝试使用 python 2.7.1 和 paramiko 1.12.0:

connection = paramiko.SSHClient()
connection.load_host_keys(os.path.expanduser(os.path.join("~", ".ssh", "known_hosts")))
connection.set_missing_host_key_policy(paramiko.AutoAddPolicy())

connection.connect(hostIP, 22, 'd', 'd')

command = 'ls'
(stdin, stdout, stderr) = connection.exec_command(command)
response = stdout.readlines()
errormsg = stderr.read()

但我收到此错误消息:

Traceback (most recent call last):<br>
File "./ssh.py", line 32, in <module><br>
(stdin, stdout, stderr) = connection.exec_command(command)<br>
File "/app/python/2.7.1/LMWP3/lib/python2.7/site-packages/paramiko/client.py", line 379, in exec_command<br>
chan.exec_command(command)<br>
File "/app/python/2.7.1/LMWP3/lib/python2.7/site-packages/paramiko/channel.py", line 218, in exec_command<br>
self._wait_for_event()<br>
File "/app/python/2.7.1/LMWP3/lib/python2.7/site-packages/paramiko/channel.py", line 1129, in _wait_for_event<br>
raise e<br>
paramiko.SSHException: Channel closed.

最佳答案

import paramiko
s = paramiko.SSHClient()
s.set_missing_host_key_policy(paramiko.AutoAddPolicy())
conn = s.connect(hostIP, username ='root', password='rootpassword', port=22)

command = 'pwd'
(stdin, stdout, stderr) = s.exec_command(command)

print stdout.read()
s.close()

这应该可以在 Linux 上使用 root 用户正常工作。如果不是,您可能传递了错误的 hostIP 值(例如:值中的引号)、用户名和密码。

关于python - paramiko.SSHException : Channel closed 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20683896/

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