I’m using Paramiko to try and crawl through a list of hosts. The code will work until one of the hosts in the list is unavailable. It produces this error.
File "remote.py", line 12, in <module>
ssh.connect(i, username='user', password='pass')
File "/usr/local/lib/python2.7/dist-packages/paramiko/client.py", line 296, in connect
sock.connect(addr)
File "/usr/lib/python2.7/socket.py", line 224, in meth
return getattr(self._sock,name)(*args)
socket.error: [Errno 113] No route to host
My Code:
#!/usr/bin/python
import paramiko
host = ['cpu1','cpu2','cpu3']
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
for i in host:
str(i)
ssh.connect(i, username='user', password='pass')
stdin, stdout, stderr = ssh.exec_command('w')
print stdout.readlines()
ssh.close()
I’m wanting the script to run through and execute the command. If it cannot connect to the host then skip and go to the next. Am I missing an if statement with some Paramiko parameters?
you can try catching the exception, handling and maybe continuing if you so desire.
Always properly handle exceptions don’t blindly escape them using
pass, it can lead to hard to find bugs.