2012-05-30 43 views
3

決定給Python的首次嘗試,很抱歉,如果答案是顯而易見的Python的 - 的paramiko收到錯誤「對象有沒有屬性‘get_fingerprint’

我試圖創建一個使用的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」對象是指我以爲我濱海ely不得不通過它到RSA密鑰的路徑,但它似乎想要一些對象。

回答

9

pkey參數應該是實際的私鑰密鑰,而不是包含密鑰的文件的名稱。請注意,pkey應該是PKey對象而不是字符串(例如private_key = paramiko.RSAKey.from_private_key_file (private_key_filename))。 而不是pkey,你可以使用key_filename參數直接傳遞文件名。

查看documentationconnect

0

如果你有你的私鑰作爲一個字符串,您可以在此上蟒3+

from io import StringIO 
ssh = paramiko.SSHClient() 

private_key = "you-private-key-here" 
pk = paramiko.RSAKey.from_private_key(private_key)) 

ssh.connect('somehost.com', username='myName', pkey= pk) 

如果你的私有密鑰存儲在一個環境變量特別有用。

相關問題