paramiko为失败的连接抛出什么错误/异常?

2024-09-28 23:27:03 发布

您现在位置:Python中文网/ 问答频道 /正文

如果失败:

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为错误的连接尝试抛出了什么样的错误。哪些是异常类,如何导入它们?


Tags: inhostparamikomyconnect错误stdinline
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

问题在于对ssh.connect()的调用。
在这种情况下,必须指定连接端口。
示例:
ssh.connect(服务器,端口=22,用户名=user,pkey=key)

那对我有用。

您可以从查看API文档开始,查看以异常结尾的所有类:

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

然后,您还应该捕获socket.error。我想这会给你带来很多东西。socket.gaierrorsocket.error的一个子类。

相关问题 更多 >