Aug-18-2017, 03:29 PM
Hello,
I am writing a Python script, using Paramiko, to ssh to servers one by one to test connectivity. The issue I am having is, if one of the server is unavailable, I get a stack trace and my program stops running there on. How do I just report something like "connection refused" and move on to another host please?
I am writing a Python script, using Paramiko, to ssh to servers one by one to test connectivity. The issue I am having is, if one of the server is unavailable, I get a stack trace and my program stops running there on. How do I just report something like "connection refused" and move on to another host please?
import logging import sys import getpass import paramiko from paramiko import client import threading, paramiko username = raw_input("Username: ") password = getpass.getpass("Password: ") class ssh: client = None shell = None transport = None def __init__(self, address): print("Connecting to server : " + address + ".") self.client = paramiko.client.SSHClient() self.client.set_missing_host_key_policy(paramiko.client.AutoAddPolicy()) self.client.connect(address, username=username, password=password, look_for_keys=False) def sendCommand(self, command): if(self.client): stdin, stdout, stderr = self.client.exec_command(command) while not stdout.channel.exit_status_ready(): #Print data whena available if stdout.channel.recv_ready(): alldata = stdout.channel.recv(1024) prevdata = b"1" while prevdata: prevdata = stdout.channel.recv(1024) alldata += prevdata print(str(alldata)) else: print("Connection not opened.") connection = ssh("host1") connection.sendCommand("uptime") connection = ssh("host2") connection.sendCommand("uptime") connection = ssh("host3")