Artificial Intelligence
Please fill the required field.

code in python, a tool that grabs WiFi information


Certainly! Below is an example of a simple Python script that retrieves WiFi information using the `subprocess` module to execute operating system commands. The implementation can vary based on the operating system (Windows, macOS, or Linux). This example will cover the following platforms: - Windows: Using `netsh` - Linux: Using `nmcli` - macOS: Using `airport` ### Python Script to Grab WiFi Information ```python import subprocess import platform import json def get_wifi_info(): os_type = platform.system() if os_type == "Windows": return get_windows_wifi_info() elif os_type == "Linux": return get_linux_wifi_info() elif os_type == "Darwin": # macOS return get_macos_wifi_info() else: raise NotImplementedError(f"OS '{os_type}' is not supported.") def get_windows_wifi_info(): try: result = subprocess.check_output("netsh wlan show interfaces", shell=True, text=True) return parse_windows_output(result) except subprocess.CalledProcessError: return {"error": "Failed to retrieve WiFi information"} def parse_windows_output(output): info = {} lines = output.splitlines() for line in lines: if ":" in line: key, value = line.split(":", 1) info[key.strip()] = value.strip() return info def get_linux_wifi_info(): try: result = subprocess.check_output("nmcli dev wifi", shell=True, text=True) return parse_linux_output(result) except subprocess.CalledProcessError: return {"error": "Failed to retrieve WiFi information"} def parse_linux_output(output): lines = output.strip().splitlines() if len(lines) < 2: return {"error": "No WiFi information available"} wifi_info = lines[1].split() return { "SSID": wifi_info[0], "Signal": wifi_info[2], "Security": wifi_info[3], } def get_macos_wifi_info(): try: result = subprocess.check_output("/System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport -I", shell=True, text=True) return parse_macos_output(result) except subprocess.CalledProcessError: return {"error": "Failed to retrieve WiFi information"} def parse_macos_output(output): info = {} lines = output.splitlines() for line in lines: if ":" in line: key, value = line.split(":", 1) info[key.strip()] = value.strip() return info if __name__ == "__main__": wifi_info = get_wifi_info() print(json.dumps(wifi_info, indent=4)) ``` ### How to Use the Script 1. **Install Python**: Ensure that Python is installed on your system (Python 3.x is recommended). 2. **Run the Script**: - Save the code into a file, for example, `wifi_info.py`. - Open a terminal or command prompt. - Navigate to the folder where the script is saved. - Run the script with `python wifi_info.py`. ### Note - **Windows**: Run the script as Administrator to avoid permission issues. - **Linux**: Make sure you have `nmcli` installed (most distributions do). - **macOS**: The airport tool is built-in and does not require installation. - This script may need adjustments based on specific requirements or details you want to retrieve. ### Disclaimer Make sure to respect privacy and legal considerations when using scripts to access network information. This code is intended for educational purposes and should only be used on networks you own or have explicit permission to access.