import os import platform import socket import sys def get_local_ip(): hostname = socket.gethostname() return socket.gethostbyname(hostname) def ping_command(ip): system_platform = platform.system().lower() if system_platform == "windows": return f"ping -n 1 -w 1000 {ip} > NUL" # Windows (suppress output) elif system_platform == "darwin": # macOS return f"ping -c 1 -W 1 {ip} > /dev/null" # Suppress output else: # Assume Linux/Unix return f"ping -c 1 -W 1 {ip} > /dev/null" # Suppress output def scan_network(local_ip): base_ip = '.'.join(local_ip.split('.')[:-1]) + '.' active_hosts = [] total_ips = 254 # Number of IPs to scan (1-254) print("Scanning the network...") for i in range(1, total_ips + 1): ip = base_ip + str(i) response = os.system(ping_command(ip)) if response == 0: active_hosts.append(ip) # Update progress bar progress = (i / total_ips) * 100 bar_length = 40 block = int(bar_length * progress // 100) progress_bar = "#" * block + "-" * (bar_length - block) sys.stdout.write(f"\r[{progress_bar}] {progress:.2f}% done") sys.stdout.flush() print() # Newline after progress bar return active_hosts if __name__ == "__main__": local_ip = get_local_ip() print(f"Your local IP address: {local_ip}") active_devices = scan_network(local_ip) print("\nActive devices in the network:") for device in active_devices: print(device)