Reputation: 51
I use this way:
servers = ["192.168.100.161", "192.168.100.162", "192.168.100.163"]
top_command = "top -b -n 1"
host_config = make_host_config(servers)
client = ParallelSSHClient(servers, timeout=2, num_retries=1, retry_delay=1, host_config=host_config)
try:
output = client.run_command(top_command, sudo=True)
print([(k, [x for x in v.stdout], [x for x in v.stderr]) for (k, v) in output.items()])
for host, host_response in output:
print(host, host_response)
except Exception as e:
print("failed to connect some host: ", e)
What do I get now: an exception.
failed to connect some host: ("Error connecting to host '%s:%s' - %s - retry %s/%s", '192.168.100.161', 22, 'timed out', 1, 1)
What I desire to get: Responses from available servers Errors from unavailable servers
How to achieve it, guys?
Upvotes: 0
Views: 478
Reputation: 52
I import the socket module to test the port I connect to before executing code against a host (ip, port). In the example below, it's a TCP test to test if port 3306 of MySQL is up. I instantiate an empty variable ouside of the function, connection_test_result, that gets populated by the test_connection function(see below). The code execution function reads in the connection_test_result and executes if the result is == 0. Maybe this will help:
def test_connection(ip, port):
global connection_test_result
socket.setdefaulttimeout(1)
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
connection_test_result= sock.connect_ex((ip, int(port)))
return connection_test_result
Upvotes: 0