Python SSH iteration across different paths

I have a JSON object that looks like this:

{UID_1: {
Jump Boxes:[jump_ip1, jump_ip2,...],
Host: [host_ip1, host_ip2,...],
...}
UID_2: {...

The authentication at the jumpboxes is done via kerberos (passwordless), the authentication at the hosts is done with password and the hosts are visible only via the jump-hosts. I do not know from the list of IPs that work, which get stuck or do not answer, and so on, so I have to find the first path that lets me open an SSH session.

What I can do is look for exit codes when sshing with the following jump hosts:

Jumpip = & # 39; & # 39;
for me in json[uid][jumpboxes]:
if os.system ('ssh {} @ {} & # 39; format (username, i))> 0:
continue
otherwise:
Jumpip = i
break

This gives me the first working Jumpbox IP without problems. However, it is not easy to password-check the exit code to establish an SSH connection with the second host.

There are several ways to open the tunnel – either with os.system () and sshpass with a session proxy (such as:
if os.system (sshpass -p {} ssh -o ProxyCommand = "ssh {} @ {} nc {} 22" {} @ {} -t {} & # 39; format (password, user , Jump, hosts)[j]Users, hosts[j], remote_cmd))> 0: .... (For the context, suppose the sshpass command looks something like this: sshpass -p password123! -o ProxyCommand = "ssh user @ jumpbox nc hostip 22" user @ hostip -t ll

This is not robust enough for me, so I go through the IPs correctly, which I am open to make suggestions.

A bit of background – the tunnel would be used to start a command without a command and then closed. The script uses multiprocessing and pool to iterate through a number of these. I will launch them and then loop their status and get the result of the remote script running on the host. I know that os.system is outdated and I should use a subprocess, but this is not really necessary for the use case, so I'm not really worried about it. I am looking for a smart way to go through the possible paths that will require a list of Jumpbox with length n and a list of hosts with length m and timeout x Seconds max n * m * x Seconds to find out, and shorten that time instead.

I also use pexpect (which uses paramiko itself) to interact with the remote hosts as soon as I find the right IPs that I need to use to open the tunnel.

Thank you in advance!