Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 17 additions & 8 deletions testping1.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import subprocess
import concurrent.futures
from tqdm import tqdm # Install with `pip install tqdm`

def is_reachable(ip, timeout=1):
Expand Down Expand Up @@ -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.")

Expand Down
Loading