2013-12-19 76 views
1

我嘗試使用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

回答

1
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的錯誤值(例如:值中的引號),用戶名,密碼。