📜  Python – 获取系统已连接的所有 Wifi 设备

📅  最后修改于: 2022-05-13 01:55:26.309000             🧑  作者: Mango

Python – 获取系统已连接的所有 Wifi 设备

在本文中,我们将了解如何使用系统连接的所有 wifi 网络。 Wi-Fi 是一种无线网络技术,它允许计算机(笔记本电脑和台式机)、移动设备(智能手机和可穿戴设备)以及其他设备(打印机和摄像机)等设备与 Internet 连接。
为此,我们将使用 subprocess 模块。 Python(2.x 和 3.x)中存在的 subprocess 模块用于通过创建新进程来通过Python代码运行新的应用程序或程序。它还有助于获取输入/输出/错误管道以及各种命令的退出代码。
为了获取有关 wifi 设备系统连接到的信息,我们将使用下面给出的命令

subprocess.check_output(['netsh', 'wlan', 'show', 'profiles'])

该命令将以字节格式返回元数据,以获取我们必须解码的网络名称,并对其进行过滤以获得所需的输出。
下面是实现

Python3
# importing subprocess
import subprocess
 
# getting meta data of the wifi network
meta_data = subprocess.check_output(['netsh', 'wlan', 'show', 'profiles'])
 
# decoding meta data from byte to string
data = meta_data.decode('utf-8', errors ="backslashreplace")
 
# splitting data by line by line
# string to list
data = data.split('\n')
 
# creating a list of wifi names
names = []
 
# traverse the list
for i in data:
     
    # find "All User Profile" in each item
    # as this item will have the wifi name
    if "All User Profile" in i :
         
        # if found split the item
        # in order to get only the name
        i = i.split(":")
         
        # item at index 1 will be the wifi name
        i = i[1]
         
        # formatting the name
        # first and last chracter is use less
        i = i[1:-1]
         
        # appending the wifi name in the list
        names.append(i)
 
# printing the wifi names
print("All wifi that system has connected to are ")
print("-----------------------------------------")
for name in names:
    print(name)


输出 :

All wifi that system has connected to are 
-----------------------------------------
Engineer_5GHz
Engineer
honor
Redmi
Ayush
BiGX-cmtqaGFtYjc
UERJTTBV8e0GUmVkbWkg 2
DESKTOP-F32H70N 5009
UERJTTBV8e0GUmVkbWkg
Bunns
Hogwarts
Cgc wireless
Moto G (5) Plus 8691
AndroidAP
AndroidAPab7e
roshan
Svj?
Hey
AndroidAP202
JARVIS
B6NO-wq5hamF0IGt1bWHCrg
CDAC