'ParallelSSHClient - Python - Handle authentication errors

I have a problem which I didn't found the solution here.

I am working with SSHClient for connecting to multiple servers. But, if there is 1 server in the list that I cannot access with my username and password (SSH), it's throwing me an exception.

I've tried to work with try and except but it didn't work. Here is my original code:

from pssh.clients import ParallelSSHClient
import configparser

res = []
servers = ['test1', 'test2', 'test3', 'test4']
client = ParallelSSHClient(servers, user='test', password='test')
output = client.run_command('service ntpd status')
client.join(output)

for host_out in output:
    for line in host_out.stdout:
        if 'running' or 'Running' in line:
            continue
        else:
            res.append(host_out.host + ' is not running')
if res:
    return res
else:
    return "All servers are running"

server test2 isn't accessible with my username so the script is throwing me an exception and failing the script:

pssh.exceptions.AuthenticationError: No authentication methods succeeded

How can I continue the script without the server test2 (if it is not accessible)



Solution 1:[1]

try something like this:

from pssh.clients import ParallelSSHClient
from pssh.config import HostConfig

host_config = [
    HostConfig(user='user',
               password='pwd'),
    HostConfig(user='user',
               password='pwd'),
    HostConfig(user='user',
               password='pwd')
]

servers = ['10.97.180.90', '10.97.180.99', "10.97.180.88"] 

client = ParallelSSHClient(servers, host_config=host_config, num_retries=1, timeout=3)

output = client.run_command('uname -a', stop_on_errors=False, timeout=3)
for host_output in output:
    try:
        for line in host_output.stdout:
            print(line)
        exit_code = host_output.exit_code
    except TypeError:
        print("timeOut...")
        

And output looks like this:

Linux hostName-001 3.10.0-957.21.3.el7.x86_64 #1 SMP Fri Jun 14 02:54:29 EDT 2019 x86_64 x86_64 x86_64 GNU/Linux
Linux hostName-003 3.10.0-1160.15.2.el7.x86_64 #1 SMP Thu Jan 21 16:15:07 EST 2021 x86_64 x86_64 x86_64 GNU/Linux
timeOut...

More info can be found here on stackoverflow: Python parallel-ssh run_command does not timeout when using pssh.clients

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 First Last