决定第一次尝试使用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 key 的路径即可,但似乎需要一些对象。

最佳答案

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

有关connect的信息,请参见documentation

10-08 00:01