import sys import subprocess import time from concurrent.futures import ThreadPoolExecutor, as_completed def ping_host(ip): result = subprocess.run( ["ping", "-c", "1", "-W", "1", ip], stdout=subprocess.DEVNULL ) return ip if result.returncode == 0 else None def ping_sweep(network_prefix, max_threads=100): live_hosts = [] with ThreadPoolExecutor(max_workers=max_threads) as executor: futures = [executor.submit(ping_host, f"{network_prefix}.{i}") for i in range(1, 256)] for future in as_completed(futures): result = future.result() if result: print(f"{result} is up.") live_hosts.append(result) return live_hosts # ---- Entry Point ---- if __name__ == "__main__": if len(sys.argv) < 2: print("Usage: python3 pingsweep.py ") print("Example: python3 pingsweep.py 192.168.1") sys.exit(1) # Assume prefix like "192.168.1" prefix = sys.argv[1] timestamp = time.strftime('%Y%m%d-%H%M%S') filename = f'{prefix}.0_24_{timestamp}.txt' print(f"Scanning {prefix}.1 to {prefix}.255 ...") hosts = ping_sweep(prefix) print("\nLive hosts:") for host in hosts: print(host) with open(filename, 'w') as f: for host in hosts: f.write(host + '\n') print(f'Saved live hosts to {filename}')