使用paramiko中 not found in known_hosts的错误解决

执行代码如下
$ cat paramiko-demo1.py
#!/usr/bin/env python
import paramiko
hostname='192.168.4.71'
username='username'
password='password'
paramiko.util.log_to_file('syslogin.log')
ssh=paramiko.SSHClient()
ssh.load_system_host_keys()
ssh.connect(hostname=hostname,username=username,password=password)
stdin,stdout,stderr=ssh.exec_command('free -m')
print stdout.read()
ssh.close

执行报错
$ python paramiko-demo1.py
Traceback (most recent call last):
  File "paramiko-demo1.py", line 10, in
    ssh.connect(hostname=hostname,username=username,password=password)
  File "/usr/lib/python2.7/site-packages/paramiko/client.py", line 361, in connect
    server_key)
  File "/usr/lib/python2.7/site-packages/paramiko/client.py", line 648, in missing_host_key
    raise SSHException('Server %r not found in known_hosts' % hostname)
paramiko.ssh_exception.SSHException: Server '192.168.4.71' not found in known_hosts

进入python环境,调试代码如下
$ python
Python 2.7.5 (default, Aug  4 2017, 00:39:18)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-16)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import paramiko
>>> ssh=paramiko.SSHClient()
>>> ssh.connect(hostname='192.168.4.71',username='username',password='password')
Traceback (most recent call last):
  File "", line 1, in
  File "/usr/lib/python2.7/site-packages/paramiko/client.py", line 361, in connect
    server_key)
  File "/usr/lib/python2.7/site-packages/paramiko/client.py", line 648, in missing_host_key
    raise SSHException('Server %r not found in known_hosts' % hostname)
paramiko.ssh_exception.SSHException: Server '192.168.4.71' not found in known_hosts
>>> ssh.load_system_host_keys()
>>> ssh.connect(hostname='192.168.4.71',username='clusteruser',password='clusteruser6se4')
Traceback (most recent call last):
  File "", line 1, in
  File "/usr/lib/python2.7/site-packages/paramiko/client.py", line 361, in connect
    server_key)
  File "/usr/lib/python2.7/site-packages/paramiko/client.py", line 648, in missing_host_key
    raise SSHException('Server %r not found in known_hosts' % hostname)
paramiko.ssh_exception.SSHException: Server '192.168.4.71' not found in known_hosts
>>> ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
>>> ssh.connect(hostname='192.168.4.71',port=22,username='username',password='password',compress=True)
>>> stdin,stdout,stderr=ssh.exec_command('hostname;uptime')
>>> print stdout.read()
192.168.4.71
 21:49:19 up 15 days, 10:27,  0 users,  load average: 0.01, 0.03, 0.05

>>> ssh.close()

显然加入
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(hostname='192.168.4.71',port=22,username='username',password='password',compress=True)
这两条代码后,完成了功能。
set_missing_host_key_policy方法,是制定连接远程主机没有本地密钥或HostKeys对象是的策略,有三种策略:
1、AutoAddPolicy,自动添加主机名及主机密钥到本地HostKeys对象,并保存,不依赖load_system_host_keys()的配置,即使~/.ssh/known_hosts不存在也不产生影响。

2、WarningPolicy,用于记录一个未知的主机密钥的python警告,并接受它,功能上与AutoAddPolicy相似,但未知主机会有告警

3、RejectPolicy,自动拒绝不知的主机名和密钥,依赖 load_system_host_keys()的配置

你可能感兴趣的:(网络,编程)