python在windows电脑找回WiFi密码
注意:不是破解密码,在能无线上网的情况子下,找出保存过的密码。代码及命令解释很详细:
import subprocess
import re
import ctypes
import sysdef is_admin():"""检查当前 Python 脚本是否以管理员权限运行。返回:bool: 如果以管理员权限运行返回 True,否则返回 False。"""try:return ctypes.windll.shell32.IsUserAnAdmin()except (AttributeError, OSError):# 处理访问动态链接库函数失败或系统错误return Falsedef get_current_wifi_name():# 定义一个名为 get_current_wifi_name 的函数,该函数不接受任何参数。try:# 使用 try 语句块来捕获可能出现的异常。# 使用系统默认编码解码output = subprocess.check_output('netsh wlan show interface', shell=True).decode(sys.getdefaultencoding(), errors='replace')# subprocess.check_output 用于在 Python 中执行外部命令。# 'netsh wlan show interface' 是 Windows 系统的命令,用于显示当前 Wi-Fi 接口的信息。# shell=True 表示通过系统的 shell 来执行命令。# decode(sys.getdefaultencoding(), errors='replace') 将命令执行结果的字节流按系统默认编码解码为字符串,# errors='replace' 表示遇到无法解码的字符时用替换字符替代。# 查找当前连接的Wi-Fi名称match = re.search(r"SSID\s*:\s*(.*)", output)# re.search 是 Python 中 re 模块的函数,用于在字符串中搜索匹配正则表达式的第一个位置。# r"SSID\s*:\s*(.*)" 是正则表达式,含义如下:# "SSID" 匹配字面字符串 "SSID";# \s* 匹配零个或多个空白字符;# ":" 匹配冒号;# \s* 再次匹配零个或多个空白字符;# (.*) 是一个捕获组,匹配任意字符(除换行符外)零次或多次,用于捕获 Wi-Fi 名称。if match:return match.group(1).strip()# 如果匹配成功,match.group(1) 获取捕获组中的内容,即 Wi-Fi 名称。# strip() 方法去除字符串两端的空白字符后返回。else:print("无法获取当前连接的Wi-Fi名称")return None# 如果匹配失败,打印提示信息并返回 None。except Exception as e:print(f"发生错误: {e}")return None# 捕获所有异常,打印错误信息并返回 None。def get_wifi_password(ssid):# 定义一个名为 get_wifi_password 的函数,接收一个参数 ssid,代表 Wi-Fi 网络的名称。if not ssid:# 检查传入的 ssid 是否为空(即 None、空字符串等假值)。return None# 如果 ssid 为空,直接返回 None。try:# 使用 try 语句块来捕获可能出现的异常。# 使用系统默认编码解码output = subprocess.check_output(f'netsh wlan show profile name="{ssid}" key=clear', shell=True).decode(sys.getdefaultencoding(), errors='replace')# subprocess.check_output 用于在 Python 中执行外部命令。# f'netsh wlan show profile name="{ssid}" key=clear' 是 Windows 系统的命令,用于显示指定 Wi-Fi 网络的详细配置信息,包括密码(key=clear 表示显示明文密码)。# shell=True 表示通过系统的 shell 来执行命令。# decode(sys.getdefaultencoding(), errors='replace') 将命令执行结果的字节流按系统默认编码解码为字符串,# errors='replace' 表示遇到无法解码的字符时用替换字符替代。print("netsh 命令完整输出:")print(output) # 打印完整输出# 查找Wi-Fi密码,使用更灵活的正则表达式match = re.search(r"关键内容\s*:\s*(.*)", output)# re.search 是 Python 中 re 模块的函数,用于在字符串中搜索匹配正则表达式的第一个位置。# r"关键内容\s*:\s*(.*)" 是正则表达式,含义如下:# "关键内容" 匹配中文的 "关键内容" 字符串;# \s* 匹配零个或多个空白字符;# ":" 匹配冒号;# \s* 再次匹配零个或多个空白字符;# (.*) 是一个捕获组,匹配任意字符(除换行符外)零次或多次,用于捕获 Wi-Fi 密码。if match:return match.group(1).strip()# 如果匹配成功,match.group(1) 获取捕获组中的内容,即 Wi-Fi 密码。# strip() 方法去除字符串两端的空白字符后返回。else:print(f"未找到Wi-Fi '{ssid}'的密码,可能该网络没有保存密码或为开放网络。")return None# 如果匹配失败,打印提示信息并返回 None。except subprocess.CalledProcessError as e:print(f"执行命令时出错: {e.output.decode(sys.getdefaultencoding(), errors='replace')}")return None# 捕获 subprocess.check_output 执行命令失败时抛出的 CalledProcessError 异常,# 打印错误信息并返回 None。except Exception as e:print(f"发生错误: {e}")return None# 捕获其他所有异常,打印错误信息并返回 None。if __name__ == "__main__":if not is_admin():ctypes.windll.shell32.ShellExecuteW(None, "runas", sys.executable, " ".join(sys.argv), None, 1)else:current_wifi = get_current_wifi_name()if current_wifi:print(f"当前连接的Wi-Fi: {current_wifi}")password = get_wifi_password(current_wifi)if password:print(f"Wi-Fi密码: {password}")else:print("未能获取密码。请确保该网络保存了密码。")else:print("无法确定当前连接的Wi-Fi。")# 等待用户输入后再关闭窗口input("按回车键退出...")