I am trying to do a an automated Ethernet bandwidth test on all the ports of a router. My set up involves connecting from a Windows PC with 1 connection to a router to a Linux PC with 7 connections. I'm using the Paramiko module to SSH into the the Linux PC to start an Ethernet testm which works fine.
However, I wanted to see what would happen if I removed one of the Ethernet cables in the test. This causes the program to crash, saying "socket operation was attempted to an unreachable host". I want to be able to avoid this error. Is there a way to check if the connection is possible so that I don't get a crash? Ideally, I would like the code to skip over the bad connection and proceed to the next cable. Here is a snippet of the code used:
ssh.connect( hostname = target_host[i] , username = un, password = pwd )
stdin, stdout, stderr = ssh.exec_command('iperf -c 192.168.0.98')
t_read=stdout.read()
read[i]=t_read[360:375]
raw_speed[i]=t_read[360:364]
address[i]=t_read[221:233]
print('Receieved data on cable %s from %s via IP: %s at %s \n'%(Cable[i],WMI_Port[i],address[i],read[i]))
stdin, stdout, stderr = ssh.exec_command('sudo ifconfig %s down'%(target_eth[i]))
print ('Disabling %s : \n\n'%(WMI_Port[i]))
ssh.close()