paramiko.SSHException:通道关闭

问题描述:

我尝试使用Python 2.7.1和的paramiko 1.12.0:paramiko.SSHException:通道关闭

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. 
+0

您的连接语法不正确。您应该先搜索谷歌样本代码。 – 2013-12-19 14:33:40

+0

你是什么意思?关于我看到的正确的示例代码。 – user2968409

+0

我已经连接 connection.connect(hostIP,username ='d',password ='d') 版本。 – user2968409

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的错误值(例如:值中的引号),用户名,密码。