2010-11-09 79 views
9
我有連接到設備用的paramiko(版本1.7.6-2)SSH客戶端問題

的paramiko的AuthenticationException問題

$ python 
Python 2.6.5 (r265:79063, Apr 16 2010, 13:09:56) 
[GCC 4.4.3] on linux2 
Type "help", "copyright", "credits" or "license" for more information. 
>>> import paramiko 
>>> ssh = paramiko.SSHClient() 
>>> ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) 
>>> ssh.connect("123.0.0.1", username="root", password=None) 
Traceback (most recent call last): 
    File "<stdin>", line 1, in <module> 
    File "/usr/lib/pymodules/python2.6/paramiko/client.py", line 327, in connect 
    self._auth(username, password, pkey, key_filenames, allow_agent, look_for_keys) 
    File "/usr/lib/pymodules/python2.6/paramiko/client.py", line 481, in _auth 
    raise saved_exception 
paramiko.AuthenticationException: Authentication failed. 
>>> 

當我使用ssh命令行,它工作正常:

ssh [email protected] 


BusyBox v1.12.1 (2010-11-03 13:18:46 EDT) built-in shell (ash) 
Enter 'help' for a list of built-in commands. 

# 

以前有人看過這個嗎?

編輯1

這裏是ssh命令的詳細的輸出:

:~$ ssh -v [email protected] 
OpenSSH_5.3p1 Debian-3ubuntu4, OpenSSL 0.9.8k 25 Mar 2009 
debug1: Reading configuration data /etc/ssh/ssh_config 
debug1: Applying options for * 
debug1: Connecting to 123.0.0.1 [123.0.0.1] port 22. 
debug1: Connection established. 
debug1: identity file /home/waffleman/.ssh/identity type -1 
debug1: identity file /home/waffleman/.ssh/id_rsa type -1 
debug1: identity file /home/waffleman/.ssh/id_dsa type -1 
debug1: Remote protocol version 2.0, remote software version OpenSSH_5.1 
debug1: match: OpenSSH_5.1 pat OpenSSH* 
debug1: Enabling compatibility mode for protocol 2.0 
debug1: Local version string SSH-2.0-OpenSSH_5.3p1 Debian-3ubuntu4 
debug1: SSH2_MSG_KEXINIT sent 
debug1: SSH2_MSG_KEXINIT received 
debug1: kex: server->client aes128-ctr hmac-md5 none 
debug1: kex: client->server aes128-ctr hmac-md5 none 
debug1: SSH2_MSG_KEX_DH_GEX_REQUEST(1024<1024<8192) sent 
debug1: expecting SSH2_MSG_KEX_DH_GEX_GROUP 
debug1: SSH2_MSG_KEX_DH_GEX_INIT sent 
debug1: expecting SSH2_MSG_KEX_DH_GEX_REPLY 
debug1: Host '123.0.0.1' is known and matches the RSA host key. 
debug1: Found key in /home/waffleman/.ssh/known_hosts:3 
debug1: ssh_rsa_verify: signature correct 
debug1: SSH2_MSG_NEWKEYS sent 
debug1: expecting SSH2_MSG_NEWKEYS 
debug1: SSH2_MSG_NEWKEYS received 
debug1: SSH2_MSG_SERVICE_REQUEST sent 
debug1: SSH2_MSG_SERVICE_ACCEPT received 
debug1: Authentication succeeded (none). 
debug1: channel 0: new [client-session] 
debug1: Requesting [email protected] 
debug1: Entering interactive session. 
debug1: Sending environment. 
debug1: Sending env LANG = en_US.utf8 

編輯2 這裏是Python輸出與調試輸出:

Python 2.6.5 (r265:79063, Apr 16 2010, 13:09:56) 
[GCC 4.4.3] on linux2 
Type "help", "copyright", "credits" or "license" for more information. 
>>> import paramiko, os 
>>> paramiko.common.logging.basicConfig(level=paramiko.common.DEBUG) 
>>> ssh = paramiko.SSHClient() 
>>> ssh.load_system_host_keys() 
>>> ssh.load_host_keys(os.path.expanduser('~/.ssh/known_hosts')) 
>>> ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) 
>>> ssh.connect("123.0.0.1", username='root', password=None) 
DEBUG:paramiko.transport:starting thread (client mode): 0x928756cL 
INFO:paramiko.transport:Connected (version 2.0, client OpenSSH_5.1) 
DEBUG:paramiko.transport:kex algos:['diffie-hellman-group-exchange-sha256', 'diffie-hellman-group-exchange-sha1', 'diffie-hellman-group14-sha1', 'diffie-hellman-group1-sha1'] server key:['ssh-rsa', 'ssh-dss'] client encrypt:['aes128-cbc', '3des-cbc', 'blowfish-cbc', 'cast128-cbc', 'arcfour128', 'arcfour256', 'arcfour', 'aes192-cbc', 'aes256-cbc', '[email protected]', 'aes128-ctr', 'aes192-ctr', 'aes256-ctr'] server encrypt:['aes128-cbc', '3des-cbc', 'blowfish-cbc', 'cast128-cbc', 'arcfour128', 'arcfour256', 'arcfour', 'aes192-cbc', 'aes256-cbc', '[email protected]', 'aes128-ctr', 'aes192-ctr', 'aes256-ctr'] client mac:['hmac-md5', 'hmac-sha1', '[email protected]', 'hmac-ripemd160', '[email protected]', 'hmac-sha1-96', 'hmac-md5-96'] server mac:['hmac-md5', 'hmac-sha1', '[email protected]', 'hmac-ripemd160', '[email protected]', 'hmac-sha1-96', 'hmac-md5-96'] client compress:['none', '[email protected]'] server compress:['none', '[email protected]'] client lang:[''] server lang:[''] kex follows?False 
DEBUG:paramiko.transport:Ciphers agreed: local=aes128-ctr, remote=aes128-ctr 
DEBUG:paramiko.transport:using kex diffie-hellman-group1-sha1; server key type ssh-rsa; cipher: local aes128-ctr, remote aes128-ctr; mac: local hmac-sha1, remote hmac-sha1; compression: local none, remote none 
DEBUG:paramiko.transport:Switch to new keys ... 
DEBUG:paramiko.transport:Trying discovered key b945197b1de1207d9aa0663f01888c3c in /home/waffleman/.ssh/id_rsa 
DEBUG:paramiko.transport:userauth is OK 
INFO:paramiko.transport:Authentication (publickey) failed. 
Traceback (most recent call last): 
    File "<stdin>", line 1, in <module> 
    File "/usr/lib/pymodules/python2.6/paramiko/client.py", line 327, in connect 
    self._auth(username, password, pkey, key_filenames, allow_agent, look_for_keys) 
    File "/usr/lib/pymodules/python2.6/paramiko/client.py", line 481, in _auth 
    raise saved_exception 
paramiko.AuthenticationException: Authentication failed. 
>>> 
+0

我試過這個來連接運行嵌入式linux的QNAP TS-409。工作很好。 – hughdbrown 2010-11-09 15:42:46

+0

我想你可以用'ssh root @ 123.0.0.1'來查看ssh是如何連接的。 – 2010-11-09 17:01:44

+0

做「ssh -v [email protected]」並查看SSH是否使用公鑰認證或其他認證方法? – 2010-11-09 17:19:14

回答

7

遠程設備上的ssh服務器拒絕您的身份驗證。確保您使用的是正確的密鑰,並且該設備沒有任何其他訪問限制。沒有來自服務器的日誌很難說出發生了什麼。

[編輯]我只是回頭看看你的輸出,你使用None認證進行身份驗證。這通常是不允許的,並且用於確定服務器允許哪些auth方法。您的服務器可能使用基於主機的身份驗證(或根本沒有!)。

由於auth_none()很少使用,因此無法從SSHClient類訪問,因此您需要直接使用Transport

transport.auth_none('root') 
+0

這就是它...棘手。謝謝。 – waffleman 2010-11-30 20:39:58

+0

嗨@waffleman,我也是門相同的問題..所以我把transport.auth_none('根')行代碼,但現在得到錯誤爲。NameError:名稱'運輸'沒有定義 – 2011-10-25 18:02:51

+0

@Spike:你聲明運輸?運輸= ssh.get_transport() – waffleman 2011-11-16 19:41:57

0

paramiko's SSHClientload_system_host_keys方法您可以使用加載用戶特定的一組密鑰。例如在文檔解釋中,它需要在連接到服務器之前運行。

+0

沒有運氣。我也嘗試刪除known_hosts,看看是否有任何影響。 – waffleman 2010-11-09 19:05:10

+0

@waffleman:你嘗試過'load_host_keys'方法嗎? – SilentGhost 2010-11-09 19:36:28

+0

也沒有工作 – waffleman 2010-11-09 20:25:53

0

當服務器使用AD身份驗證時,出現類似的錯誤。我認爲這是一個paramiko的錯誤。我已經瞭解到,我必須在使用paramiko之前設置ssh密鑰。

2

確保將公鑰和私鑰文件(以及可能包含的文件夾)的權限設置爲非常嚴格(即chmod 600 id_rsa)。事實證明,這是必需的(由操作系統?)使用這些文件作爲SSH密鑰。從我有用的同事那裏找到了這個:) 還要確保你使用給定ssh密鑰的正確用戶名。

-3

我試圖刪除文件夾〜/ SSH,那麼它的工作以及

+3

你不應該刪除'.ssh'文件夾 – dazedconfused 2014-12-09 10:18:48

3

由於在這個問題上非常晚的後續行動,我相信我也陷入了相同的問題waffleman,在上下文一個受限制的網絡。

關於在Transport對象上使用auth_none的提示很有幫助,但是我發現自己對如何實現這一點感到有些困惑。事情是,至少在今天,我不能得到SSHClient對象的Transport對象,直到它連接;但它不會連接在第一位...

因此,如果這對他人有用,我的工作就在下面。我只是重寫_auth方法。

好吧,這是脆弱的,因爲_auth是私人的東西。我的其他選擇 - 實際上仍然是 - 手動創建TransportChannel對象,但暫時我覺得我更好,但所有這些仍然處於底層。

from paramiko import SSHClient 

class SSHClient_noauth(SSHClient): 

    def _auth(self, username, *args): 
     self._transport.auth_none(username) 
     return