IT博客汇
  • 首页
  • 精华
  • 技术
  • 设计
  • 资讯
  • 扯淡
  • 权利声明
  • 登录 注册

    使用Python获取电脑保存的wifi密码并保存到记事本(Windows下食用)

    52txr发表于 2024-02-03 18:09:16
    love 0

    机器人LOGO AI 摘要 讯飞星光LOGO
    本文介绍了如何在Windows系统下使用Python获取电脑上保存的WiFi密码,并提供了相关代码。用户需确保有足够权限运行此脚本,并合法使用,不要用于未经授权的网络访问。
    内容由讯飞星火GPT生成,经过人工审核后发布

    有时候找了半天不知道如何获得自己的电脑上之前输入的密码,作为一个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文件就是如此:

    获得到的密码文件

    注意事项

    请合法使用此脚本,仅在你有权访问的计算机上运行,且不要用于未经授权的计算机。



沪ICP备19023445号-2号
友情链接