我尝试使用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.
答案 0 :(得分: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值(例如:值中的引号),用户名,密码。