使用 python 获取设备中存储的 wifi 密码

Get stored wifi passwords in a device using python

我试过 运行 这个脚本来获取设备中的所有 wifi 密码,但是当我 运行 没有任何反应。 就连 运行 都没有错误。 os 是 windows 这是脚本

import subprocess
import re


command_output = subprocess.run(["netsh", "wlan", "show", "profiles"], capture_output = True).stdout.decode()

profile_names = (re.findall("All User Profile       : (.*)\r", command_output))

wifi_list = list()

if len(profile_names) !=0:
    for name in profile_names:

        wifi_profile = dict()

        profile_info = subprocess.run(["netsh", "wlan", "show", "profile", name], capture_output = True).stdout.decode()
 
        if re.search("Security Key      : Absent", profile_info):
            continue
        else:

            wifi_profile["ssid"] = name

            profile_info_pass = subprocess.run(["netsh", "wlan", "show", "profile", name, "key=clear"], capture_output = True).stdout.decode()

            password = re.search("Key Content       : (.*)\r", profile_info_pass)

            if password == None:
                wifi_profile["password"] = None

            else:
                wifi_profile["password"] = password[1]

            wifi_list.append(wifi_profile)


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


我曾经研究过这个检查这个代码

import subprocess

data = subprocess.check_output(['netsh', 'wlan', 'show', 'profiles']).decode('utf-8').split('\n')
profiles = [i.split(":")[1][1:-1] for i in data if "All User Profile" in i]
for i in profiles:
    results = subprocess.check_output(['netsh', 'wlan', 'show', 'profile', i, 'key=clear']).decode('utf-8').split('\n')
    results = [b.split(":")[1][1:-1] for b in results if "Key Content" in b]
    try:
        print ("{:<30}|  {:<}".format(i, results[0]))
    except IndexError:
        print ("{:<30}|  {:<}".format(i, ""))
input("")

在我的系统上,re.search 行的空格太多。尝试从每个空格中删除两个空格,例如"Security Key : Absent" 变为 "Security Key : Absent"。更好的解决方案是更改它以应对任意数量的空格。