Windows10系统使用Python查看本机已存储的WiFi密码

Windows10系统使用Python查看本机已存储的WiFi密码

import re
import subprocess


name_list = []
# 定义要执行的命令
command = 'netsh wlan show profiles'

# 使用subprocess.run执行命令,并捕获输出
result = subprocess.run(command, shell=True, text=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)

# 检查命令是否执行成功
if result.returncode == 0:
    # 打印命令的输出结果
    for line in result.stdout.splitlines():
        if '所有用户配置文件' in line:
            match = re.search(r':\s*(.*)', line)
            wifi_name = match.group(1)
            # 将获取到的WiFi名称写入存储的目的列表
            name_list.append(wifi_name)
else:
    # 如果命令执行失败,打印错误信息
    print("Error executing command:", result.stderr)


counter_password = 0
counter_open = 0
for info in name_list:
    if ' ' in info:
        command_password = f'netsh wlan show profiles name="{info}" key=clear'
    else:
        command_password = f"netsh wlan show profiles {info} key=clear"
    # 使用subprocess.run执行命令,并捕获输出
    result_password = subprocess.run(command_password, shell=True, text=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    # 检查命令是否执行成功
    if result_password.returncode == 0:
        for lines in result_password.stdout.splitlines():
            if '关键内容' in lines:
                matchs = re.search(r':\s*(.*)', lines)
                password_info = matchs.group(1)
                print(f'WiFi名称:{info} 的密码是:{password_info}')
                counter_password += 1
            elif '开放式' in lines:
                print(f'WiFi名称:{info} 的密码是:开放WiFi,无密码!')
                counter_open += 1
    else:
        # 如果命令执行失败,打印错误信息
        print("Error executing command:", result_password.stderr)


print(f'本机已存储的WiFi总数是:{len(name_list)} \n无线WiFi加密的个数是:{counter_password}\n开发WiFi的数量是:{counter_open}')

 

posted @ 2024-12-23 21:49  博雅塔之客  阅读(1)  评论(0编辑  收藏  举报