From a4d79efd88da74940a8a3ae414700603c720f7e1 Mon Sep 17 00:00:00 2001 From: Jayant Seth Date: Fri, 26 May 2023 15:36:31 +0530 Subject: [PATCH] optimized ping method --- ping.py | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/ping.py b/ping.py index bacabea..8107053 100644 --- a/ping.py +++ b/ping.py @@ -1,5 +1,9 @@ -import subprocess import platform +import subprocess +import threading +import time + +results = {} def ping(ip): """ @@ -18,17 +22,23 @@ def ping(ip): # We will check if the output contains the string '0% packet loss' or not output_list = output.split(packet_loss_string) if len(output_list) > 1: + results[ip] = "UP" return "UP" else: + results[ip] = "DOWN" return "DOWN" - + def ping_all(ips): """ pings all ips and returns a dictionary """ - results = {} + threads = [] for ip in ips: - result = ping(ip) - results[ip] = result - return results \ No newline at end of file + thread = threading.Thread(target=ping, args=(ip,)) + threads.append(thread) + thread.start() + time.sleep(.2) + for thread in threads: + thread.join() + return results