在Python中使用WlanScan强制wifi扫描

2024-10-02 20:31:36 发布

您现在位置:Python中文网/ 问答频道 /正文

我想知道如何从python执行WlanScan函数来启动无线网络扫描。我正在使用python模块win32wifi。它需要使用WlanOpenHandle获得的句柄和接口GUID pInterfaceGuid。我不知道怎么弄到这个指南。任何帮助都将不胜感激。在

How do I get this pInterfaceGuid


Tags: 模块函数指南句柄guid无线网络wlanscanwlanopenhandle
2条回答

我安装了Win32WiFi模块,在简单检查了@Castorix提供的URL之后(所有需要的信息都可以在[MS.Docs]: wlanapi.h header)和源代码中找到,我就能够编写这个小示例了。在

代码.py

#!/usr/bin/env python3

import sys
from win32wifi import Win32Wifi as ww


def main():
    interfaces = ww.getWirelessInterfaces()
    print("WLAN Interfaces: {:d}".format(len(interfaces)))
    handle = ww.WlanOpenHandle()
    for idx , interface in enumerate(interfaces):
        print("\n  {:d}\n  GUID: [{:s}]\n  Description: [{:s}]".format(idx, interface.guid_string, interface.description))
        try:
            scan_result = ww.WlanScan(handle, interface.guid)
        except:
            print(sys.exc_info())
            continue
        print("\n  Scan result: {:d}".format(scan_result))
    ww.WlanCloseHandle(handle)


if __name__ == "__main__":
    print("Python {:s} on {:s}\n".format(sys.version, sys.platform))
    main()
    print("\nDone.")

输出

[cfati@CFATI-5510-0:e:\Work\Dev\StackOverflow\q056701614]> "e:\Work\Dev\VEnvs\py_064_03.07.03_test0\Scripts\python.exe" code.py
Python 3.7.3 (v3.7.3:ef4ec6ed12, Mar 25 2019, 22:22:05) [MSC v.1916 64 bit (AMD64)] on win32

WLAN Interfaces: 1

  0
  GUID: [{0C58E048-BC0B-4D5F-A21F-FCD4E4B31806}]
  Description: [Intel(R) Dual Band Wireless-AC 8260]

  Scan result: 0

Done.

@EDIT0

根据[SO]: Unable to get all available networks using WlanGetAvailableNetworkList in Python (@CristiFati's answer)更新了代码。它现在适用于具有多个WLAN适配器的计算机

您将得到带有WlanEnumInterfaces的Guid,它返回带有WLAN_INTERFACE_INFO structureInterfaceGuid成员的WLAN_INTERFACE_INFO_LIST structure

相关问题 更多 >