Saturday, July 23, 2022

[SOLVED] What error/exception does Paramiko throw for failed connects?

Issue

If this fails:

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

I get a traceback like:

  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

I cant figure what kind/kinds of errors Paramiko throws for bad connect attempts. Which are the exception classes and how can I import them?


Solution

You can start by looking at the API documentation, for all classes ending in Exception:

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

Then, you should also catch socket.error. I think that will get you pretty much everything. socket.gaierror is a subclass of socket.error, for example.



Answered By - djc
Answer Checked By - Mildred Charles (WPSolving Admin)