'SSHClient'对象在ssh.connect()上没有属性'known_hosts'

时间:2016-06-08 21:05:25

标签: python ssh paramiko

我正在尝试使用以下代码连接ssh服务器

ssh = paramiko.SSHClient()
ssh.load_system_host_keys()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.load_host_keys(os.path.expanduser(os.path.join("~", ".ssh", "known_hosts"))
ssh.connect("10.1.3.2", username = "root")

但是我收到以下错误消息

Exception during testMethod !!
Traceback (most recent call last):
  File "/usr/local/lib/python2.7/dist-packages/Qb/QbUnittest/QbUnittest.py", line 624, in __call__
    testMethod()
  File "testDropBear_send_file.py", line 31, in runMe
    ssh.connect("10.1.3.2", username = "root")
  File "/usr/local/lib/python2.7/dist-packages/paramiko-1.12.0-py2.7.egg/paramiko/client.py", line 326, in connect
    self._policy.missing_host_key(self, server_hostkey_name, server_key)
  File "/usr/local/lib/python2.7/dist-packages/paramiko-1.12.0-py2.7.egg/paramiko/client.py", line 71, in missing_host_key
    client.save_host_keys(client._host_keys_filename)
  File "/usr/local/lib/python2.7/dist-packages/paramiko-1.12.0-py2.7.egg/paramiko/client.py", line 192, in save_host_keys
    if self.known_hosts is not None:
AttributeError: 'SSHClient' object has no attribute 'known_hosts'

有人可以帮我理解是什么问题吗?

2 个答案:

答案 0 :(得分:1)

这是known bug,修复程序已发布到1.12.1版;你似乎在使用1.12.0。尝试升级到最新版本:

pip install --upgrade paramiko

答案 1 :(得分:1)

快速修复:

ssh = paramiko.SSHClient()
ssh.known_hosts = None
...
...

bug已修复here