有时候找了半天不知道如何获得自己的电脑上之前输入的密码,作为一个Python爱好者,当然是使用Python一件获取!这个显然不是什么什么难事!Show Time!
import subprocess
import locale
def get_wifi_passwords():
# 尝试获取系统默认编码
default_encoding = locale.getpreferredencoding(False)
# 使用subprocess执行命令,获取系统中保存的WiFi配置文件名
profiles_data = subprocess.check_output('netsh wlan show profiles').decode(default_encoding).split('\n')
profiles = [i.split(":")[1][1:-1] for i in profiles_data if "所有用户配置文件" in i or "All User Profile" in i]
# 打开(或创建)一个文本文件以写入WiFi密码
with open("wifi_passwords.txt", "w", encoding="utf-8") as file:
for profile in profiles:
try:
# 获取特定WiFi配置文件的详细信息,包括密码
wifi_data = subprocess.check_output(['netsh', 'wlan', 'show', 'profile', profile, 'key=clear']).decode(default_encoding).split('\n')
# 在输出中查找密码字段
wifi_password = [i.split(":")[1][1:-1] for i in wifi_data if "关键内容" in i or "Key Content" in i]
if wifi_password:
# 如果找到密码,写入文件
file.write(f'Profile: {profile}, Password: {wifi_password[0]}\n')
else:
# 如果没有找到密码,说明它没有设置或无法检索
file.write(f'Profile: {profile}, Password: 未设置或无法检索\n')
except subprocess.CalledProcessError:
# 如果命令执行失败,记录错误
file.write(f'Could not read password for {profile}\n')
# 调用函数
get_wifi_passwords()
确保在具有足够权限的环境中运行此脚本,因为读取WiFi密码通常需要管理员权限。运行脚本后,你可以在脚本所在目录查找wifi_passwords.txt
文件,以查看输出结果。
比如我打开这个txt文件就是如此:
请合法使用此脚本,仅在你有权访问的计算机上运行,且不要用于未经授权的计算机。