<p>在我的主机上输出您的脚本并进行此更改:
stdin,stdout,stderr=ssh.exec_命令('ls-1/root | head-n 5')</p>
<p>1.py年</p>
<pre><code>import paramiko
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
target_host = '127.0.0.1'
target_port = 22
target_port = 22
pwd = 'password'
un = 'root'
ssh.connect( hostname = target_host , username = un, password = pwd )
stdin, stdout, stderr = ssh.exec_command('ls -1 /root|head -n 5')
print "STDOUT:\n%s\n\nSTDERR:\n%s\n" %( stdout.read(), stderr.read() )
# For Python3
# print("STDOUT:\n%s\n\nSTDERR:\n%s\n" %( stdout.read(), stderr.read() ))
</code></pre>
<h2>Python1.py</h2>
<pre><code>STDOUT:
~
1
1421750672-TWya15.png
1.py
7
STDERR:
</code></pre>
<p>我认为您需要ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())避免</p>
<pre><code>The authenticity of host 'localhost (::1)' can't be
established.
RSA key fingerprint is
22:fb:16:3c:24:7f:60:99:4f:f4:57:d6:d1:09:9e:28.
Are you sure you want to continue connecting
(yes/no)?
import paramiko
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(
paramiko.AutoAddPolicy())
ssh.connect('127.0.0.1', username='jesse',
password='lol')
</code></pre>
<p><a href="http://jessenoller.com/blog/2009/02/05/ssh-programming-with-paramiko-completely-different" rel="nofollow noreferrer">SSH PROGRAMMING WITH PARAMIKO | COMPLETELY DIFFERENT</a></p>