python获取电脑连接过的所有WIFI密码

我打包的可执行程序,可以获取电脑wifi文件打印到自己桌面

百度网盘 请输入提取码

提取码: 7777

今天看到一篇博文写的获取电脑连接过的WIFI密码,遇到一些编码bug,我优化了一下。

原理:

netsh wlan show profiles

这个可以查看电脑连接过的WIFI名称

netsh wlan show profile name="xxx" key=clear

这样可以查看具体的密码

但是这样一个一个查不方便,写个循环就行了

源代码

import subprocess
import chardet
import os

def decode_str(encoded_str):
    # 检测编码
    detected_encoding = chardet.detect(encoded_str)['encoding']
    # 使用检测到的编码进行解码
    try:
        decoded_str = encoded_str.decode(detected_encoding)
    except UnicodeDecodeError:
        # 尝试使用 'utf-8' 编码进行解码
        try:
            decoded_str = encoded_str.decode('utf-8')
        except UnicodeDecodeError:
            # 尝试使用 'gbk' 编码进行解码
            decoded_str = encoded_str.decode('gbk')
    return decoded_str
# 执行netsh命令获取Wi-Fi密码
result = subprocess.check_output('netsh wlan show profiles', shell=True)
profiles = []
for line in decode_str(result).split('\n'):
    if "所有用户配置文件 :" in line:
        profiles.append(line.split(':')[1].strip())
text = []
for profile in profiles:
    try:
        password_result = subprocess.check_output(f'netsh wlan show profile name="{profile}" key=clear',shell=True)
        # print(password_result)
        for line in decode_str(password_result).split('\n'):
            if "关键内容" in line:
                text.append('\n{:<40}{:>30}'.format('WIFI名称:'+profile,'WIFI密码:'+line.split(':')[1].strip()))
    except:
        print(f'无法获取{profile}的密码')
        text.append(f'\n无法获取{profile}的密码')
def saveDate(datalist,savepath):
    if not os.path.isdir(savepath):  # 判断是否存在该文件夹,若不存在则创建
        os.mkdir(savepath)  # 创建
    with open(savepath + "\\" + "wifi密码.txt", 'w', encoding='utf-8') as file:  # 打开这个文件
        file.write(datalist)  # 打印文字


desktop_path = os.path.join(os.path.expanduser("~"), "Desktop")  # 找到用户桌面的路径
savepath = os.path.join(desktop_path, "WiFi密码")  # 文件夹名称
saveDate(''.join(text), savepath)

猜你喜欢

转载自blog.csdn.net/qq_57420582/article/details/134250508