1. import locale
    2. import subprocess
    3. from FileUtil import *
    4. from OtherUtil import *
    5. from RequestUtil import *
    6. def get_local_lang():
    7. return locale.getdefaultlocale() # ('zh_CN', 'cp936')
    8. def run():
    9. loc_lang = get_local_lang()
    10. # 代码中用到的正则匹配模式字符串,提取出来以便不同语言系统使用,默认支持中文英文,其他语言需要更改匹配语句
    11. if loc_lang[0] == "zh_CN":
    12. re_pattern = ["所有用户配置文件 : (.*)\r", "安全密钥 : 不存在", "关键内容 : (.*)\r"]
    13. else:
    14. re_pattern = ["All User Profile : (.*)\r", "Security key : Absent",
    15. "Key Content : (.*)\r"]
    16. # 如果 capture_output 设为 true,stdout 和 stderr 将会被捕获
    17. # netsh wlan show profiles 111 key=clear
    18. cmd_output = subprocess.run(["netsh", "wlan", "show", "profiles"], capture_output=True).stdout.decode('gbk')
    19. # print(cmd_output)
    20. wifi_names = (re.findall(re_pattern[0], cmd_output))
    21. # print(wifi_names)
    22. wifi_list = []
    23. if len(wifi_names) != 0:
    24. for name in wifi_names:
    25. # 每一个wifi的信息存储在一个字典里
    26. wifi_profile = {}
    27. profile_info = subprocess.run(["netsh", "wlan", "show", "profiles", name],
    28. capture_output=True).stdout.decode('gbk')
    29. # print(profile_info)
    30. # 判断wifi密码是否存储在windows计算机里,不存在则忽略
    31. if re.search(re_pattern[1], profile_info):
    32. continue
    33. else:
    34. wifi_profile["ssid"] = name
    35. # 密码存在时,加上命令参数“key=clear”显示wifi密码
    36. profile_info_pass = subprocess.run(["netsh", "wlan", "show", "profiles", name, "key=clear"],
    37. capture_output=True).stdout.decode('gbk')
    38. password = re.search(re_pattern[2], profile_info_pass)
    39. # print(password)
    40. if not password:
    41. wifi_profile["password"] = None
    42. else:
    43. wifi_profile["password"] = password[1]
    44. wifi_list.append(wifi_profile)
    45. wifi_content = ""
    46. for i in range(len(wifi_list)):
    47. print(wifi_list[i])
    48. wifi_content += "名称: " + wifi_list[i]["ssid"] + "\n密码: " + wifi_list[i]["password"] + "\n\n"
    49. save_text("wifi.txt", wifi_content)
    50. print("导出结束,已保存至wifi.txt")
    51. time.sleep(3)
    52. if __name__ == '__main__':
    53. run()