📜  使用Python获取保存的 Wifi 密码(1)

📅  最后修改于: 2023-12-03 14:49:50.940000             🧑  作者: Mango

使用Python获取保存的Wifi密码

在这篇文章中,我们将探讨如何使用Python获取保存在计算机上的Wifi密码。这个功能有很多应用场景,比如当你忘记了自己的Wifi密码时,可以使用这个程序获取。

实现步骤
  1. 导入所需模块
import subprocess
import re
  1. 执行命令获取Wifi列表
wifi_names = []
results = subprocess.check_output(["netsh", "wlan", "show", "profiles"]).decode("utf-8", errors="ignore")
results = results.split("\n")
  1. 使用正则表达式提取Wifi名称
for result in results:
    if "All User Profile" in result:
        wifi_names.append(result.split(":")[1].strip())
  1. 执行命令获取Wifi密码
passwords = {}
for name in wifi_names:
    results = subprocess.check_output(["netsh", "wlan", "show", "profile", name, "key=clear"]).decode("utf-8", errors="ignore")
    results = results.split("\n")
    results = [line.strip() for line in results]
    results = list(filter(None, results))
    try:
        ssid_index = results.index("SSID name              : {}".format(name))
        key_index = results.index("Key Content            :")
        password = results[key_index + 1]
        passwords[name] = password
    except ValueError:
        passwords[name] = ""
代码片段
import subprocess
import re

wifi_names = []
results = subprocess.check_output(["netsh", "wlan", "show", "profiles"]).decode("utf-8", errors="ignore")
results = results.split("\n")

for result in results:
    if "All User Profile" in result:
        wifi_names.append(result.split(":")[1].strip())

passwords = {}
for name in wifi_names:
    results = subprocess.check_output(["netsh", "wlan", "show", "profile", name, "key=clear"]).decode("utf-8", errors="ignore")
    results = results.split("\n")
    results = [line.strip() for line in results]
    results = list(filter(None, results))
    try:
        ssid_index = results.index("SSID name              : {}".format(name))
        key_index = results.index("Key Content            :")
        password = results[key_index + 1]
        passwords[name] = password
    except ValueError:
        passwords[name] = ""
结论

通过以上步骤,我们已经成功地获取了保存在计算机上的Wifi密码。如果你需要保存这些密码,你可以把它们保存到本地文件中。但是要注意,这些密码应该仅用于个人使用,不应该传播给他人,以保护网络安全。