paramiko 安装

yum install python-paramiko -y
yum install openssh-server openssh-clients -y

报错

Traceback (most recent call last):
  File "ssh.py", line 10, in <module>
    ssh.connect(hostname=hostname,port=port,username=username,password=password)
  File "/usr/lib/python2.6/site-packages/paramiko/client.py", line 306, in connect
    self._policy.missing_host_key(self, hostname, server_key)
  File "/usr/lib/python2.6/site-packages/paramiko/client.py", line 83, in missing_host_key
    raise SSHException('Unknown server %s' % hostname)
paramiko.SSHException: Unknown server 192.168.2.11


解决方法一

1、创建 ~/.ssh/known_hosts

2、远程连接一次

eg

#!/usr/bin/env python
import paramiko
hostname='192.168.2.11'
username='root'
password='oracle'
port=22
 
ssh=paramiko.SSHClient()
ssh.load_system_host_keys()
ssh.connect(hostname=hostname,port=port,username=username,password=password)
stdin,stdout,stderr=ssh.exec_command('ls /')
print stdout.read()
ssh.close()


解决方法二

将ssh.load_system_host_keys()替换为set_missing_host_key_policy(paramiko.AutoAddPolicy())

eg

#!/usr/bin/env python
import paramiko
hostname='10.13.106.36'
port=22
username='root'
password='centos'
if __name__=='__main__':
        paramiko.util.log_to_file('paramiko.log')
        s=paramiko.SSHClient()
        s.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        s.connect(hostname,port,username,password)
        stdin,stdout,stderr=s.exec_command('ifconfig')
        print stdout.read()
        s.close()


参考博文:http://emrys411876027.qj67.wshost.cc/python/69.html