Force wifi scan using WlanScan in Python

Question:

I want to know how to execute the WlanScan function from python to initiate a wireless network scan. I am using python module win32wifi. It requires a handle obtained using WlanOpenHandle and an interface GUID pInterfaceGuid. I have no idea how to get this GUID. any help would be appreciated.

How do I get this pInterfaceGuid

Asked By: HaMAD

||

Answers:

You get the Guid with WlanEnumInterfaces which returns WLAN_INTERFACE_INFO_LIST structure with WLAN_INTERFACE_INFO structure and InterfaceGuid member

Answered By: Castorix

I installed the Win32WiFi module and after a brief check of URLs provided by @Castorix (all required info can be found at [MS.Docs]: wlanapi.h header), and the source code, I was able to write this small example.

code00.py:

#!/usr/bin/env python3

import sys

from win32wifi import Win32Wifi as ww


def main(*argv):
    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} {:03d}bit on {:s}n".format(" ".join(elem.strip() for elem in sys.version.split("n")),
                                                   64 if sys.maxsize > 0x100000000 else 32, sys.platform))
    rc = main(*sys.argv[1:])
    print("nDone.")
    sys.exit(rc)

Output:

[cfati@CFATI-5510-0:e:WorkDevStackOverflowq056701614]> "e:WorkDevVEnvspy_pc064_03.07.06_test0Scriptspython.exe" code00.py
Python 3.7.6 (tags/v3.7.6:43364a7ae0, Dec 19 2019, 00:42:30) [MSC v.1916 64 bit (AMD64)] 064bit on win32

WLAN Interfaces: 1

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

  Scan result: 0

Done.


Update #0

Updated the code according to [SO]: Unable to get all available networks using WlanGetAvailableNetworkList in Python (@CristiFati’s answer). It will now work for computers that have more than one WLAN adapter.

Answered By: CristiFati