Python lấy danh sách SSID wifi linux

def get_wifi_ssid_rows[self] -> List[List[str]]:
        rows: List[List[str]] = list[]
        results: List[Dict[str, Union[int, str]]] = list[]
        for bssid in self.wifi_instance.bssids.keys[]:
            try:
                assert 'essid' in self.wifi_instance.bssids[bssid].keys[] and \
                       'signal' in self.wifi_instance.bssids[bssid].keys[] and \
                       'channel' in self.wifi_instance.bssids[bssid].keys[] and \
                       'enc' in self.wifi_instance.bssids[bssid].keys[] and \
                       'cipher' in self.wifi_instance.bssids[bssid].keys[] and \
                       'auth' in self.wifi_instance.bssids[bssid].keys[] and \
                       'clients' in self.wifi_instance.bssids[bssid].keys[], 'Bad AP'

                assert self.wifi_instance.bssids[bssid]['enc'] != 'UNKNOWN', 'Bad Encryption'
                assert self.wifi_instance.bssids[bssid]['cipher'] != 'UNKNOWN', 'Bad Cipher'
                assert self.wifi_instance.bssids[bssid]['auth'] != 'UNKNOWN', 'Bad Authentication'
                if self.wifi_channel != -1:
                    assert self.wifi_instance.bssids[bssid]['channel'] == self.wifi_channel, 'Bad WiFi channel'

                results.append[{
                    'essid': self.wifi_instance.bssids[bssid]['essid'],
                    'bssid': bssid,
                    'signal': self.wifi_instance.bssids[bssid]['signal'],
                    'channel': self.wifi_instance.bssids[bssid]['channel'],
                    'encryption': self.wifi_instance.bssids[bssid]['enc'] + ' ' +
                                  self.wifi_instance.bssids[bssid]['auth'] + ' ' +
                                  self.wifi_instance.bssids[bssid]['cipher'],
                    'clients': len[self.wifi_instance.bssids[bssid]['clients']]}]

            except AssertionError:
                pass

        sorted_results: List[Dict[str, Union[int, str]]] = sorted[results, key=lambda k: k['signal'], reverse=True]
        for sorted_result in sorted_results:
            rows.append[[sorted_result['essid'],
                         sorted_result['bssid'],
                         sorted_result['signal'],
                         sorted_result['channel'],
                         sorted_result['encryption'],
                         sorted_result['clients']]]
            if 'WPA' in sorted_result['encryption'] and \
                    sorted_result['bssid'] not in self.wifi_instance.pmkid_authentications.keys[]:
                self.tm_instance.add_task[self.wifi_instance.send_association_request,
                                          sorted_result['bssid'],
                                          sorted_result['essid'],
                                          False]
        return rows

# endregion


# region Main function 

Nếu bạn nhập

data = subprocess.check_output[['netsh', 'wlan', 'show', 'profiles']].decode['utf-8'].split['\n']
4 trong cmd, bạn sẽ được hiển thị cấu hình cho các kết nối wifi mà máy tính của bạn đã lưu trữ

Sau đó, nếu bạn nhập

data = subprocess.check_output[['netsh', 'wlan', 'show', 'profiles']].decode['utf-8'].split['\n']
0, đầu ra được cung cấp sẽ chứa khóa mạng là mật khẩu WiFi

Lấy mật khẩu

Đầu tiên nhập quy trình con, đây là mô-đun chúng ta sẽ sử dụng để tương tác với cmd

import subprocess

Tiếp theo, lấy đầu ra cho lệnh "netsh wlan show profiles" bằng cách sử dụng quy trình con. check_output[]. Sau đó, giải mã đầu ra bằng utf-8 và chia chuỗi theo ký tự xuống dòng để lấy từng dòng trong một chuỗi riêng biệt

data = subprocess.check_output[['netsh', 'wlan', 'show', 'profiles']].decode['utf-8'].split['\n']

Bây giờ chúng tôi có một danh sách các chuỗi, chúng tôi có thể nhận được các dòng chỉ chứa "Tất cả hồ sơ người dùng". Với những dòng này, sau đó chúng ta cần chia nó bằng dấu '. ', lấy phía bên tay phải và xóa ký tự đầu tiên và cuối cùng

profiles = [i.split[":"][1][1:-1] for i in data if "All User Profile" in i]

Bây giờ, biến a chứa tên cấu hình WiFi, chúng ta có thể nhận đầu ra cho lệnh "netsh wlan show profile {Profile Name} key=clear" bằng cách sử dụng quy trình con. check_output[] một lần nữa cho một cấu hình cụ thể trong khi lặp qua tất cả các cấu hình

for i in profiles:
    results = subprocess.check_output[['netsh', 'wlan', 'show', 'profile', i, 'key=clear']].decode['utf-8'].split['\n']

Vẫn trong vòng lặp, hãy tìm các dòng có chứa "Nội dung chính", chia cho '. ' và xóa ký tự đầu tiên và cuối cùng giống như trước đây

    results = [b.split[":"][1][1:-1] for b in results if "Key Content" in b]

Bây giờ chúng ta sẽ có một danh sách chứa một chuỗi là khóa cấu hình cụ thể. Ở đây bạn chỉ có thể sử dụng một câu lệnh in đơn giản nhưng tôi chỉ định dạng nó một chút

    try:
        print ["{:

Chủ Đề