I have a server that I send 4 ICMP pings to using Python. My code looks like this:
command = ['ping', '-c', '4', '192.168.1.8']
proc = subprocess.run(command)
if proc.returncode == 0:
print("Alive")
else:
print("Dead")
In this code, each time 4 ping requests will be sent out and only after receiving the reply from all 4 will the return code be checked. However, the modification that I want to make is as soon as any one of the ping requests succeed, I want to immediately declare it as "Alive" and move on. If all 4 fail, then we mark the server as "Dead". The reason I want to make this modification is that I am pinging a lot of servers and waiting for 4 replies from each server when the first successful reply means its alive makes the script run longer. I believe this modification will make the script complete faster. How can I achieve this?