python获取连接过的WiFi密码-多线程进阶版

解决中文空格无法获取做了修改
添加多线程处理提高速度

# -*- coding: utf-8 -*-
# @time: 2022/7/21 14:28
# @Author: JCS
# @Email:[email protected]
# @File :get_wifi.py
# @Softwre: PyCharm
import os
import subprocess

# 获取wifi列表
from threading import Thread


class Get_wifi_pw(Thread):
    def __init__(self, queue):
        Thread.__init__(self)
        self.queue = queue

    def run(self):
        while True:
            wifi_data = self.queue.get()
            try:
                get_wifi_password(wifi_data)
            except:
                print('未知异常')
            finally:
                self.queue.task_done()


global wifilist
wifilist = []


def get_wifi_info():
    output = os.popen(f'netsh wlan show profiles').read().split('\n')
    wifis = [line.split(' : ')[1] for line in output if "所有用户配置文件" in line]
    return wifis


def get_wifi_password(wifi):
    cmd = fr'netsh wlan show profile name="{wifi}" key=clear'
    obj = subprocess.Popen((cmd), shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    results = obj.stdout.read().decode("gbk", "ignore").split("\n")
    len_list = len(results)
    obj.stdout.close()
    for line in results:
        try:
            if "关键内容" in line:
                txt = line.split(':')[1][1:-1]
                wifilist.append(f'wifi账号:{wifi},密码:{txt}')
            elif "开放式" in line:
                wifilist.append(f'wifi账号:{wifi},密码:"开放式!"')
        except:
            wifilist.append(f'wifi账号:{wifi},密码:"获取失败未知错误!"')

def run():
    wifi_info_list = get_wifi_info()
    wifi_info_list_len = len(wifi_info_list)
    if wifi_info_list_len == len(wifilist):
        return wifilist
    else:
        import queue
        queue = queue.Queue()
        for x in range(20):
            worker = Get_wifi_pw(queue)
            worker.daemon = True
            worker.start()
        for i in get_wifi_info():
            queue.put(i)

        queue.join()
        return wifilist


if __name__ == '__main__':
    print(run())

你可能感兴趣的:(Python,python,开发语言)