Search code examples
pythonwifi

How can I get a list of all WLANs my computer can see with Python?


I have tried the following:

#!/usr/bin/env python

import NetworkManager


def get_ssids():
    ssids = []
    for x in NetworkManager.Settings.ListConnections():
        conn = x.GetSettings()
        if '802-11-wireless' in conn and 'ssid' in conn['802-11-wireless']:
            ssids.append(conn['802-11-wireless']['ssid'])
            logging.info(conn)
    return ssids


def main():
    print("Found the following SSIDs:")
    for ssid in get_ssids():
        print("* %s" % ssid)


if __name__ == '__main__':
    main()

But that shows the SSIDs of all networks I was connected to, not of the networks I currently see.


Solution

  • Author of said library here :-)

    import NetworkManager
    
    for dev in NetworkManager.NetworkManager.GetDevices():
        if dev.DeviceType != NetworkManager.NM_DEVICE_TYPE_WIFI:
            continue
        for ap in dev.SpecificDevice().GetAccessPoints():
            print ap.Ssid