|
| 1 | +import subprocess, platform, re |
| 2 | +from colorama import init, Fore |
| 3 | + |
| 4 | +init() |
| 5 | + |
| 6 | + |
| 7 | +def list_open_networks(): |
| 8 | + # Get the name of the operating system. |
| 9 | + os_name = platform.system() |
| 10 | + |
| 11 | + # Check if the OS is Windows. |
| 12 | + if os_name == "Windows": |
| 13 | + # Command to list Wi-Fi networks on Windows. |
| 14 | + list_networks_command = 'netsh wlan show networks' |
| 15 | + try: |
| 16 | + # Execute the command and capture the output. |
| 17 | + output = subprocess.check_output(list_networks_command, shell=True, text=True) |
| 18 | + networks = [] |
| 19 | + |
| 20 | + # Parse the output to find open Wi-Fi networks. |
| 21 | + for line in output.splitlines(): |
| 22 | + if "SSID" in line: |
| 23 | + # Extract the SSID (Wi-Fi network name). |
| 24 | + ssid = line.split(":")[1].strip() |
| 25 | + elif "Authentication" in line and "Open" in line: |
| 26 | + # Check if the Wi-Fi network has open authentication. |
| 27 | + networks.append(ssid) |
| 28 | + |
| 29 | + # Check if any open networks were found. |
| 30 | + if len(networks) > 0: |
| 31 | + # Print a message for open networks with colored output. |
| 32 | + print(f'{Fore.LIGHTMAGENTA_EX}[+] Open Wifi networks in range: \n') |
| 33 | + for each_network in networks: |
| 34 | + print(f"{Fore.GREEN}[+] {each_network}") |
| 35 | + else: |
| 36 | + # Print a message if no open networks were found. |
| 37 | + print(f"{Fore.RED}[-] No open wifi networks in range") |
| 38 | + |
| 39 | + except subprocess.CalledProcessError as e: |
| 40 | + # Handle any errors that occur during the execution of the command. |
| 41 | + print(f"{Fore.RED}Error: {e}") |
| 42 | + # Return an empty list to indicate that no networks were found. |
| 43 | + return [] |
| 44 | + |
| 45 | + elif os_name == "Linux": |
| 46 | + try: |
| 47 | + # Run nmcli to list available Wi-Fi networks. |
| 48 | + result = subprocess.run(["nmcli", "--fields", "SECURITY,SSID", "device", "wifi", "list"], |
| 49 | + stdout=subprocess.PIPE, |
| 50 | + text=True, check=True) |
| 51 | + |
| 52 | + # Access the captured stdout. |
| 53 | + output = result.stdout.strip() |
| 54 | + |
| 55 | + # Define a regex pattern to capture SSID and Security. |
| 56 | + pattern = re.compile(r'^(?P<security>[^\s]+)\s+(?P<ssid>.+)$', re.MULTILINE) |
| 57 | + |
| 58 | + # Find all matches in the output. |
| 59 | + matches = pattern.finditer(output) |
| 60 | + |
| 61 | + # Skip the first match, which is the header. |
| 62 | + next(matches, None) |
| 63 | + print(f"{Fore.LIGHTMAGENTA_EX}[+] Open Wifi networks in range: \n") |
| 64 | + # Loop through all matches (results) |
| 65 | + for match in matches: |
| 66 | + security = match.group('security') |
| 67 | + ssid = match.group('ssid') |
| 68 | + full_match = f"{Fore.GREEN}[+] SSID: {ssid} -------> Security: {security}" |
| 69 | + # Check if the indicator of an open network in our Full match (result). |
| 70 | + if "Security: --" in full_match: |
| 71 | + print(f"{Fore.GREEN}[+] {ssid}") |
| 72 | + else: |
| 73 | + print(f"{Fore.RED}[-] No open Wifi networks in range.") |
| 74 | + |
| 75 | + except subprocess.CalledProcessError as e: |
| 76 | + print(f"Error running nmcli: {e}") |
| 77 | + except Exception as e: |
| 78 | + print(f"An error occurred: {e}") |
| 79 | + |
| 80 | + else: |
| 81 | + print(f"{Fore.RED}Unsupported operating system.") |
| 82 | + return [] |
| 83 | + |
| 84 | + |
| 85 | +# Call the function. |
| 86 | +list_open_networks() |
| 87 | + |
| 88 | + |
0 commit comments