获取Windows的Wifi密码-Python

有时候想要查看电脑的WiFi密码很是麻烦,本Python脚本帮助获取系统里存储过的所有WiFi密码
其原理是自动化了netsh wlan show profiles查询的过程

# subprocess 模块允许我们启动一个新进程,并连接到它们的输入/输出/错误管道,从而获取返回值
import subprocess
import re

# 用于判断OS的语言
import locale
loc_lang = locale.getdefaultlocale()
# print(loc_lang[0])

# 代码中用到的正则匹配模式字符串,提取出来以便不同语言系统使用,默认支持中文英文,其他语言需要更改匹配语句
if loc_lang[0] == "zh_CN":
    re_pattern = ["所有用户配置文件 : (.*)\r", "安全密钥               : 不存在", "关键内容            : (.*)\r"]
else:
    re_pattern = ["All User Profile     : (.*)\r", "Security key           : Absent", "Key Content            : (.*)\r"]

# 如果 capture_output 设为 true,stdout 和 stderr 将会被捕获
cmd_output = subprocess.run(["netsh", "wlan", "show", "profiles"], capture_output=True).stdout.decode('gbk')
# print(cmd_output)
wifi_names = (re.findall(re_pattern[0], cmd_output))
# print(wifi_names)
wifi_list = []
if len(wifi_names) != 0:
    for name in wifi_names:
        # 每一个wifi的信息存储在一个字典里
        wifi_profile = {}
        profile_info = subprocess.run(["netsh", "wlan", "show", "profiles", name],
                                      capture_output=True).stdout.decode('gbk')
        # print(profile_info)
        # 判断wifi密码是否存储在windows计算机里,不存在则忽略
        if re.search(re_pattern[1], profile_info):
            continue
        else:
            wifi_profile["ssid"] = name
            # 密码存在时,加上命令参数“key=clear”显示wifi密码
            profile_info_pass = subprocess.run(["netsh", "wlan", "show", "profiles", name, "key=clear"], 
                                    capture_output=True).stdout.decode('gbk')
            password = re.search(re_pattern[2], profile_info_pass)
            # print(password)
            if not password:
                wifi_profile["password"] = None
            else:
                wifi_profile["password"] = password[1]
        wifi_list.append(wifi_profile)

for i in range(len(wifi_list)):
    print(wifi_list[i])

运行效果

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值