diff --git a/testping1.py b/testping1.py index 6925ac1..7512d27 100644 --- a/testping1.py +++ b/testping1.py @@ -1,4 +1,5 @@ import subprocess +import concurrent.futures from tqdm import tqdm # Install with `pip install tqdm` def is_reachable(ip, timeout=1): @@ -26,14 +27,22 @@ def is_reachable(ip, timeout=1): # Ensure you have permission to scan this subnet! total_ips = int(end_ip.split(".")[-1]) - int(start_ip.split(".")[-1]) + 1 - with tqdm(total=total_ips, desc="Scanning network...") as pbar: # Progress bar - for i in range(1, total_ips + 1): - ip_address = f"{start_ip.split('.')[0]}.{start_ip.split('.')[1]}.{start_ip.split('.')[2]}.{i}" - pbar.set_description(f"Pinging {ip_address}...") # Update progress indicator - - if is_reachable(ip_address): - print(f"Device reachable at: {ip_address}") - pbar.update(1) # Update progress bar + + ips_to_scan = [f"{start_ip.split('.')[0]}.{start_ip.split('.')[1]}.{start_ip.split('.')[2]}.{i}" for i in range(1, total_ips + 1)] + + # ⚡ Bolt: Parallelize network scanning using ThreadPoolExecutor + # Reduces scan time significantly by performing pings concurrently instead of sequentially. + # Time complexity with respect to network delay improves from O(N) to O(N / workers). + with concurrent.futures.ThreadPoolExecutor(max_workers=50) as executor: + with tqdm(total=total_ips, desc="Scanning network...") as pbar: # Progress bar + futures = {executor.submit(is_reachable, ip): ip for ip in ips_to_scan} + for future in concurrent.futures.as_completed(futures): + ip_address = futures[future] + pbar.set_description(f"Pinging {ip_address}...") # Update progress indicator + + if future.result(): + print(f"Device reachable at: {ip_address}") + pbar.update(1) # Update progress bar print("Scanning complete.")