【问题标题】:Python - Paramiko Getting error "object has no attribute "get_fingerprint"Python - Paramiko 获取错误“对象没有属性”get_fingerprint“
【发布时间】:2012-06-05 05:50:12
【问题描述】:

决定第一次尝试 Python,如果答案很明显,请见谅。

我正在尝试使用 paramiko 创建 ssh 连接。我正在使用以下代码:

#!/home/bin/python2.7

import paramiko
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())

ssh.connect("somehost.com", username="myName", pkey="/home/myName/.ssh/id_rsa.pub")
stdin, stdout, stderr = ssh.exec_command("ls -l")

print stdout.readlines()
ssh.close()

很标准的东西,对吧?除了我收到此错误:

 ./test.py
Traceback (most recent call last):
File "./test.py", line 10, in <module>
ssh.connect("somehost", username="myName", pkey="/home/myName/.ssh/id_rsa.pub")
File "/home/lib/python2.7/site-packages/paramiko/client.py", line 327, in connect
self._auth(username, password, pkey, key_filenames, allow_agent, look_for_keys)
File "/home/lib/python2.7/site-packages/paramiko/client.py", line 418, in _auth
self._log(DEBUG, 'Trying SSH key %s' % hexlify(pkey.get_fingerprint()))
AttributeError: 'str' object has no attribute 'get_fingerprint'

它指的是什么“str”对象?我以为我只需要将 RSA 密钥的路径传递给它,但它似乎需要一些对象。

【问题讨论】:

    标签: python ssh paramiko public-key


    【解决方案1】:

    pkey 参数应该是实际的 private 密钥,而不是包含该密钥的文件的名称。请注意 pkey 应该是 PKey 对象而不是字符串(例如 private_key = paramiko.RSAKey.from_private_key_file (private_key_filename) )。 您可以使用 key_filename 参数代替 pkey 直接传递文件名。

    对于connect,请参阅documentation

    【讨论】:

      【解决方案2】:

      如果你有你的私钥作为字符串,你可以在 python 3+ 上这样做

      from io import StringIO
      ssh = paramiko.SSHClient()  
      
      private_key = StringIO("you-private-key-here")
      pk = paramiko.RSAKey.from_private_key(private_key)
      
      ssh.connect('somehost.com', username='myName', pkey= pk)
      

      如果您的私钥存储在环境变量中,则特别有用。

      【讨论】:

      • 我想你忘了使用 io.StringIO。我看到你引用它,所以你显然打算使用它。您当前的示例产生错误AttributeError: 'str' object has no attribute 'readlines' 。要解决此问题,请将行 private_key = "you-private-key-here" 更改为 private_key = io.StringIO("you-private-key-here")。感谢您的评论,它帮助我解决了 OP 发布的完全相同的问题。
      • 天哪,有人请编辑此答案,以减少语法错误。
      猜你喜欢
      • 2018-03-08
      • 1970-01-01
      • 1970-01-01
      • 2014-10-10
      • 2014-05-14
      • 1970-01-01
      • 1970-01-01
      • 2014-07-08
      • 2021-06-04
      相关资源
      最近更新 更多