I need a Python program I'm using to poll a remote server for SSH connectivity and notify when it is available. I am currently doing this using paramiko; attempt to connect, if failure, wait and retry until success or max retries. This works, but it's a bit clunky. Also paramiko seems to either connect or throw an error, so the only way I could see to do this was with a try/except block which is bad, bad, bad. Here is the method:
def check_ssh(self, ip, user, key_file, initial_wait=0, interval=0, retries=1):
ssh = paramiko.SSHClient()
for x in range(retries):
ssh.connect(ip, username=user, key_filename=key_file)
except Exception, e:
As mentioned in the comment by frb, a
try ... except block is a good approach to test availability of a specific service. You shouldn't use a "catch-all"
except block though, but limit it to the specific exceptions that occur if the service is unavailable.
According to documentation,
paramiko.SSHClient.connect may throw different exceptions, depending on the problem that occured while connecting. If you want to catch all those, your
try ... except block would look like this:
try: ssh.connect(ip, username=user, key_filename=key_file) return True except (BadHostKeyException, AuthenticationException, SSHException, socket.error) as e: print e sleep(interval)
If just a subset of these exceptions is relevant to your case, put only those into the tuple after