Aug-11-2023, 04:07 PM
(This post was last modified: Aug-11-2023, 04:50 PM by deanhystad.)
os.popen() is depreciated. Use subprocess.run(). I think os.popen actually uses subprocess.run(), so skip the middleman and your program will continue to run when os.popen has been retired.
subprocess.run('func').returncode gets the returncode from running func. For ping this will be 0 if successful, else some error code. This code pings multiple IPs at the same time using the ThreadPoolExecutor.
subprocess.run('func').returncode gets the returncode from running func. For ping this will be 0 if successful, else some error code. This code pings multiple IPs at the same time using the ThreadPoolExecutor.
import subprocess from concurrent.futures import ThreadPoolExecutor ips = ["10.0.0.5", "127.0.0.1", "::1"] def ping(ip): return ( ip, subprocess.run( f"ping {ip} -n 1", stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL ).returncode, ) executor = ThreadPoolExecutor(4) print(*executor.map(ping, ips))