2012-12-18 131 views
3

如果失敗:paramiko拋出連接失敗的錯誤/異常?

ssh = paramiko.SSHClient() 
ssh.connect(host, username = USER , pkey = MY_KEY, timeout = 2) 

我得到這樣一個回溯:

File "<stdin>", line 1, in <module> 
    File "<stdin>", line 7, in bs_process 
    File "/usr/lib/python2.7/site-packages/paramiko/client.py", line 282, in connect 
    for (family, socktype, proto, canonname, sockaddr) in socket.getaddrinfo(hostname, port, socket.AF_UNSPEC, socket.SOCK_STREAM): 
socket.gaierror: [Errno -2] Name or service not known 

我無法弄清楚什麼樣的/種錯誤的paramiko拋出壞連接嘗試。哪些是異常類,如何導入它們?

+0

我抓住(並重新加註)'BadHostKeyException','AuthenticationException', 'SSHException'和'socket.error'。可能還會有更多... – Ben

+0

@Ben顯然,'BadHostKeyException'和'AuthenticationException'是'SSHException'的子類。所以你只需要後者... – glglgl

+0

我以不同的方式處理它們@glglgl ...我可能不需要'SSHException',但它寫了一段時間之前我想成爲_sure _... – Ben

回答

3

接受的答案有一個斷開的鏈接。爲的paramiko文檔現在住在:

http://docs.paramiko.org/en/1.15/api/client.html#paramiko.client.SSHClient.connect

它的 「連接」 的方法將引發如下:

BadHostKeyException – if the server’s host key could not be verified 
AuthenticationException – if authentication failed 
SSHException – if there was any other error connecting or establishing an SSH session 
socket.error – if a socket error occurred while connecting 
相關問題