Created
December 8, 2024 12:13
-
-
Save galer7/56d0151dd1ad3cf6ad1972e46cdc50e0 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import sys | |
import subprocess | |
import platform | |
import qrcode | |
from qrcode.constants import ERROR_CORRECT_L | |
import re | |
import getpass | |
def get_current_wifi_macos(): | |
"""Get current WiFi details on macOS.""" | |
try: | |
# Get current SSID | |
airport_cmd = ['/System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport', '-I'] | |
airport_output = subprocess.check_output(airport_cmd).decode('utf-8') | |
ssid_match = re.search(r' SSID: (.+)$', airport_output, re.MULTILINE) | |
if not ssid_match: | |
return None, None | |
ssid = ssid_match.group(1).strip() | |
# Get password from Keychain | |
security_cmd = ['security', 'find-generic-password', '-D', 'AirPort network password', '-wa', ssid] | |
try: | |
password = subprocess.check_output(security_cmd).decode('utf-8').strip() | |
except subprocess.CalledProcessError: | |
password = None | |
return ssid, password | |
except Exception as e: | |
print(f"Error getting WiFi details: {str(e)}") | |
return None, None | |
def get_current_wifi_linux(): | |
"""Get current WiFi details on Linux.""" | |
try: | |
# Get current SSID | |
nmcli_cmd = ['nmcli', '-t', '-f', 'active,ssid', 'dev', 'wifi'] | |
nmcli_output = subprocess.check_output(nmcli_cmd).decode('utf-8') | |
for line in nmcli_output.split('\n'): | |
if line.startswith('yes:'): | |
ssid = line.split(':')[1].strip() | |
# Note: Getting password requires root privileges and varies by distro | |
return ssid, None | |
return None, None | |
except Exception as e: | |
print(f"Error getting WiFi details: {str(e)}") | |
return None, None | |
def get_current_wifi_windows(): | |
"""Get current WiFi details on Windows.""" | |
try: | |
# Get current SSID | |
netsh_cmd = ['netsh', 'wlan', 'show', 'interfaces'] | |
netsh_output = subprocess.check_output(netsh_cmd).decode('utf-8') | |
ssid_match = re.search(r'SSID\s+:\s(.+)$', netsh_output, re.MULTILINE) | |
if not ssid_match: | |
return None, None | |
ssid = ssid_match.group(1).strip() | |
# Note: Getting password requires admin privileges | |
return ssid, None | |
except Exception as e: | |
print(f"Error getting WiFi details: {str(e)}") | |
return None, None | |
def generate_wifi_qr(ssid, password, auth_type="WPA"): | |
"""Generate a WiFi QR code with the given credentials.""" | |
if not ssid: | |
print("Error: No WiFi network detected") | |
return | |
# Format the WiFi information according to standard | |
wifi_string = f"WIFI:T:{auth_type};S:{ssid};P:{password};;" | |
# Create QR code instance | |
qr = qrcode.QRCode( | |
version=1, | |
error_correction=ERROR_CORRECT_L, | |
box_size=1, | |
border=1 | |
) | |
# Add data and generate | |
qr.add_data(wifi_string) | |
qr.make(fit=True) | |
# Print QR code using ASCII characters | |
print(f"\nQR Code for WiFi network: {ssid}") | |
print("Scan this code with your smartphone's camera to connect:\n") | |
matrix = qr.get_matrix() | |
for row in matrix: | |
print("".join("█" if cell else " " for cell in row)) | |
def main(): | |
# Detect operating system | |
system = platform.system().lower() | |
# Get WiFi details based on OS | |
ssid, password = None, None | |
if system == 'darwin': # macOS | |
ssid, password = get_current_wifi_macos() | |
elif system == 'linux': | |
ssid, password = get_current_wifi_linux() | |
elif system == 'windows': | |
ssid, password = get_current_wifi_windows() | |
else: | |
print(f"Unsupported operating system: {system}") | |
sys.exit(1) | |
# If no password was found automatically, prompt for it | |
if not password: | |
if ssid: | |
print(f"Found WiFi network: {ssid}") | |
password = getpass.getpass("Enter WiFi password (input will be hidden): ") | |
else: | |
print("No WiFi network detected. Please enter details manually.") | |
ssid = input("Enter WiFi network name (SSID): ") | |
password = getpass.getpass("Enter WiFi password (input will be hidden): ") | |
# Generate QR code | |
generate_wifi_qr(ssid, password) | |
if __name__ == "__main__": | |
try: | |
main() | |
except KeyboardInterrupt: | |
print("\nOperation cancelled by user.") | |
sys.exit(1) | |
except Exception as e: | |
print(f"An error occurred: {str(e)}") | |
sys.exit(1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment