当前位置: 首页 > 面试题库 >

如何使用Python在Windows上检索附近的无线LAN网络的信号强度?

穆铭晨
2023-03-14
问题内容

如何使用Python在Windows上检索附近的无线LAN网络的信号强度?

我想显示或绘制值。


问题答案:

如果您使用的是 Windows ,则可能要 使用WLAN API ,该 API 提供了“
WlanGetAvailableNetworkList()”功能(有关用法,请参阅API文档)。我不知道有任何python包装器,WLANAPI.DLL因此您可能必须使用ctypes自己包装它。我有一个初步的脚本可以做到这一点(“
为我工作” ),但这可能很麻烦。您将需要阅读文档以了解所有字段的含义:

from ctypes import *
from ctypes.wintypes import *
from sys import exit


def customresize(array, new_size):
    return (array._type_*new_size).from_address(addressof(array))

wlanapi = windll.LoadLibrary('wlanapi.dll')

ERROR_SUCCESS = 0

class GUID(Structure):
    _fields_ = [
        ('Data1', c_ulong),
        ('Data2', c_ushort),
        ('Data3', c_ushort),
        ('Data4', c_ubyte*8),
        ]

WLAN_INTERFACE_STATE = c_uint
(wlan_interface_state_not_ready,
 wlan_interface_state_connected,
 wlan_interface_state_ad_hoc_network_formed,
 wlan_interface_state_disconnecting,
 wlan_interface_state_disconnected,
 wlan_interface_state_associating,
 wlan_interface_state_discovering,
 wlan_interface_state_authenticating) = map(WLAN_INTERFACE_STATE, range(0, 8))

class WLAN_INTERFACE_INFO(Structure):
    _fields_ = [
        ("InterfaceGuid", GUID),
        ("strInterfaceDescription", c_wchar * 256),
        ("isState", WLAN_INTERFACE_STATE)
        ]

class WLAN_INTERFACE_INFO_LIST(Structure):
    _fields_ = [
        ("NumberOfItems", DWORD),
        ("Index", DWORD),
        ("InterfaceInfo", WLAN_INTERFACE_INFO * 1)
        ]

WLAN_MAX_PHY_TYPE_NUMBER = 0x8
DOT11_SSID_MAX_LENGTH = 32
WLAN_REASON_CODE = DWORD
DOT11_BSS_TYPE = c_uint
(dot11_BSS_type_infrastructure,
 dot11_BSS_type_independent,
 dot11_BSS_type_any) = map(DOT11_BSS_TYPE, range(1, 4))
DOT11_PHY_TYPE = c_uint
dot11_phy_type_unknown      = 0
dot11_phy_type_any          = 0
dot11_phy_type_fhss         = 1
dot11_phy_type_dsss         = 2
dot11_phy_type_irbaseband   = 3
dot11_phy_type_ofdm         = 4
dot11_phy_type_hrdsss       = 5
dot11_phy_type_erp          = 6
dot11_phy_type_ht           = 7
dot11_phy_type_IHV_start    = 0x80000000
dot11_phy_type_IHV_end      = 0xffffffff

DOT11_AUTH_ALGORITHM = c_uint
DOT11_AUTH_ALGO_80211_OPEN         = 1
DOT11_AUTH_ALGO_80211_SHARED_KEY   = 2
DOT11_AUTH_ALGO_WPA                = 3
DOT11_AUTH_ALGO_WPA_PSK            = 4
DOT11_AUTH_ALGO_WPA_NONE           = 5
DOT11_AUTH_ALGO_RSNA               = 6
DOT11_AUTH_ALGO_RSNA_PSK           = 7
DOT11_AUTH_ALGO_IHV_START          = 0x80000000
DOT11_AUTH_ALGO_IHV_END            = 0xffffffff

DOT11_CIPHER_ALGORITHM = c_uint
DOT11_CIPHER_ALGO_NONE            = 0x00
DOT11_CIPHER_ALGO_WEP40           = 0x01
DOT11_CIPHER_ALGO_TKIP            = 0x02
DOT11_CIPHER_ALGO_CCMP            = 0x04
DOT11_CIPHER_ALGO_WEP104          = 0x05
DOT11_CIPHER_ALGO_WPA_USE_GROUP   = 0x100
DOT11_CIPHER_ALGO_RSN_USE_GROUP   = 0x100
DOT11_CIPHER_ALGO_WEP             = 0x101
DOT11_CIPHER_ALGO_IHV_START       = 0x80000000
DOT11_CIPHER_ALGO_IHV_END         = 0xffffffff

WLAN_AVAILABLE_NETWORK_CONNECTED = 1
WLAN_AVAILABLE_NETWORK_HAS_PROFILE = 2

WLAN_AVAILABLE_NETWORK_INCLUDE_ALL_ADHOC_PROFILES = 0x00000001
WLAN_AVAILABLE_NETWORK_INCLUDE_ALL_MANUAL_HIDDEN_PROFILES = 0x00000002

class DOT11_SSID(Structure):
    _fields_ = [
        ("SSIDLength", c_ulong),
        ("SSID", c_char * DOT11_SSID_MAX_LENGTH)
        ]

class WLAN_AVAILABLE_NETWORK(Structure):
    _fields_ = [
        ("ProfileName", c_wchar * 256),
        ("dot11Ssid", DOT11_SSID),
        ("dot11BssType", DOT11_BSS_TYPE),
        ("NumberOfBssids", c_ulong),
        ("NetworkConnectable", c_bool),
        ("wlanNotConnectableReason", WLAN_REASON_CODE),
        ("NumberOfPhyTypes", c_ulong),
        ("dot11PhyTypes", DOT11_PHY_TYPE * WLAN_MAX_PHY_TYPE_NUMBER),
        ("MorePhyTypes", c_bool),
        ("wlanSignalQuality", c_ulong),
        ("SecurityEnabled", c_bool),
        ("dot11DefaultAuthAlgorithm", DOT11_AUTH_ALGORITHM),
        ("dot11DefaultCipherAlgorithm", DOT11_CIPHER_ALGORITHM),
        ("Flags", DWORD),
        ("Reserved", DWORD)
        ]

class WLAN_AVAILABLE_NETWORK_LIST(Structure):
    _fields_ = [
        ("NumberOfItems", DWORD),
        ("Index", DWORD),
        ("Network", WLAN_AVAILABLE_NETWORK * 1)
        ]

WlanOpenHandle = wlanapi.WlanOpenHandle
WlanOpenHandle.argtypes = (DWORD, c_void_p, POINTER(DWORD), POINTER(HANDLE))
WlanOpenHandle.restype = DWORD

WlanEnumInterfaces = wlanapi.WlanEnumInterfaces
WlanEnumInterfaces.argtypes = (HANDLE, c_void_p, 
                               POINTER(POINTER(WLAN_INTERFACE_INFO_LIST)))
WlanEnumInterfaces.restype = DWORD

WlanGetAvailableNetworkList = wlanapi.WlanGetAvailableNetworkList
WlanGetAvailableNetworkList.argtypes = (HANDLE, POINTER(GUID), DWORD, c_void_p, 
                                        POINTER(POINTER(WLAN_AVAILABLE_NETWORK_LIST)))
WlanGetAvailableNetworkList.restype = DWORD

WlanFreeMemory = wlanapi.WlanFreeMemory
WlanFreeMemory.argtypes = [c_void_p]


if __name__ == '__main__':
    NegotiatedVersion = DWORD()
    ClientHandle = HANDLE()
    ret = WlanOpenHandle(1, None, byref(NegotiatedVersion), byref(ClientHandle))
    if ret != ERROR_SUCCESS:
        exit(FormatError(ret))

    # find all wireless network interfaces
    pInterfaceList = pointer(WLAN_INTERFACE_INFO_LIST())
    ret = WlanEnumInterfaces(ClientHandle, None, byref(pInterfaceList))
    if ret != ERROR_SUCCESS:
        exit(FormatError(ret))

    try:
        ifaces = customresize(pInterfaceList.contents.InterfaceInfo,
                              pInterfaceList.contents.NumberOfItems)
        # find each available network for each interface
        for iface in ifaces:
            print("Interface: {}".format(iface.strInterfaceDescription))
            pAvailableNetworkList = pointer(WLAN_AVAILABLE_NETWORK_LIST())
            ret = WlanGetAvailableNetworkList(ClientHandle, 
                                        byref(iface.InterfaceGuid),
                                        0,
                                        None,
                                        byref(pAvailableNetworkList))
            if ret != ERROR_SUCCESS:
                exit(FormatError(ret))
            try:
                avail_net_list = pAvailableNetworkList.contents
                networks = customresize(avail_net_list.Network, 
                                        avail_net_list.NumberOfItems)
                for network in networks:
                    print("SSID: {}, quality: {:2d}%".format(
                        network.dot11Ssid.SSID[:network.dot11Ssid.SSIDLength].decode(),
                        network.wlanSignalQuality))
            finally:
                WlanFreeMemory(pAvailableNetworkList)
    finally:
        WlanFreeMemory(pInterfaceList)

linux上 ,您有两种选择:

标准选项是解析的输出iwlist -scan。但是,如果您当前连接到无线局域网并且没有以root身份运行,则它仅返回当前连接的无线局域网。

如果您还需要更多,最好的方法是查询无线管理器守护程序。在现代linux上,这通常是NetworkManager,尽管wicd变得越来越流行。可以使用查询这两个管理器dbus。除非您可以控制客户端在其系统上拥有的内容,否则可能需要同时支持这两个选项或至少一个选项,并具有的后备功能iwlist



 类似资料:
  • 我试图通过使用谷歌地图的api来获取我发送的位置的附近位置。下面是url(https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=-33.8670522,151.1957362 日志e(“Res”,response.body()。getStatus()); 我是不是用正确的方式进行改造? 我的客户。爪哇: Near

  • 我有一个10.101的服务器VLAN。10.0/24,我的Docker主机是10.101。10.31. 如何在Docker主机(VM)上配置网桥网络,以便所有容器都可以直接连接到LAN网络,而无需在默认的172.17上重定向端口。0.0/16? 我尝试过搜索,但到目前为止我找到的所有howto都导致SSH会话丢失,我不得不从控制台进入VM以恢复我执行的步骤。

  • 如何在flutter应用程序中从google maps中检索我附近位置的数据

  • 我正在开发一个中断/延迟容忍网络(DTN)应用程序,它可以连接到使用Android附近的Android设备,无需认证。

  • 问题内容: 我想获得可用无线网络的列表。理想情况下,这将通过一些C调用进行,但是我不介意是否必须通过系统调用来混淆它。如果所需的C调用或程序不需要某些奇特的第三方包,甚至会更好。 互联网似乎建议我使用从命令行确实可以解决问题的技巧,但是我宁愿不需要root权限。我只想了解基础知识,什么也不想改变。 问题答案: 该无线工具包-它的iwlist是其中的一部分- 还包含一个无线工具帮助程序库。您需要包含

  • 问题内容: 如何使用Python检索网页的页面标题(标题html标签)? 问题答案: 我将始终将lxml用于此类任务。您也可以使用beautifulsoup。 根据评论进行编辑: