Hi all - I’m new here so thanks in advance for the help!
I have been trying to make a container on a Raspberry Pi that can set-up it’s own WiFi at runtime.
I came across this example which has pointed me down the correct road for configuring the Host NetworkManager via dbus.
My question though - does anyone know if it is possible to do a WiFi Scan for the Access Points it can see from the container? I normally use iwlist to do this on a normal debian instance but obviously I can’t do this from the container.
I noticed NetworkManager’s libnm has a function nm_device_wifi_request_scan but I don’t see this exposed via dbus.
You may call the RequestScan method on the org.freedesktop.NetworkManager.Device.Wireless interface. Please note that this will return immediately - before the scanning results are available.
You may subscribe to the completion of the scan request, but I do not remember the exact details. Checking the nmcli could reveal how it is done there. I think it was the last-scan property which was introduced in NM 1.12.
Alternatively you may do some heuristic blocking solution that could be easier to implement.
Hey, I got this to work using the method you outlined.
For now I am just blocking with a sleep while we scan but I imagine that I will try and use the signals later if this gets in the way.
I was also unsure of how to interpret the parameter list for RequestScan:
IN a{sv} options: Options of scan. Currently 'ssids' option with value of "aay" type is supported.
This may be a scan filter? Unsure, but sending an empty dict seems to find everything.
import time
import dbus
bus = dbus.SystemBus()
# Get a proxy for the base NetworkManager object
proxy = bus.get_object("org.freedesktop.NetworkManager", "/org/freedesktop/NetworkManager")
manager = dbus.Interface(proxy, "org.freedesktop.NetworkManager")
all_aps = []
# Get all network devices
devices = manager.GetDevices()
for d in devices:
dev_proxy = bus.get_object("org.freedesktop.NetworkManager", d)
prop_iface = dbus.Interface(dev_proxy, "org.freedesktop.DBus.Properties")
# Make sure the device is enabled before we try to use it
state = prop_iface.Get("org.freedesktop.NetworkManager.Device", "State")
if state <= 2:
continue
# Get device's type; we only want wifi devices
iface = prop_iface.Get("org.freedesktop.NetworkManager.Device", "Interface")
dtype = prop_iface.Get("org.freedesktop.NetworkManager.Device", "DeviceType")
if dtype == 2: # WiFi
# Get a proxy for the wifi interface
wifi_iface = dbus.Interface(dev_proxy, "org.freedesktop.NetworkManager.Device.Wireless")
wifi_prop_iface = dbus.Interface(dev_proxy, "org.freedesktop.DBus.Properties")
opt = dbus.Dictionary({
})
# Get all APs the card can see
wifi_iface.RequestScan(options=opt)
time.sleep(1)
aps = wifi_iface.GetAllAccessPoints()
for path in aps:
ap_proxy = bus.get_object("org.freedesktop.NetworkManager", path)
ap_prop_iface = dbus.Interface(ap_proxy, "org.freedesktop.DBus.Properties")
ssid = bytearray(ap_prop_iface.Get("org.freedesktop.NetworkManager.AccessPoint", "Ssid")).decode()
# Cache the BSSID
if not ssid in all_aps:
all_aps.append(ssid)
# and print out all APs the wifi devices can see
print("\nFound APs:")
for bssid in all_aps:
print(bssid)
Empty dict should do the job for the scan options. This is a scan filter indeed for scanning for particular networks.
I think you may increase the sleep value a bit, since one second will not be enough for the scan operation to complete. It usually takes a couple of seconds from what I remember. I think 3 or even 5 will be a safe bet.
You may put some protection at your SSID decode call, since if by any chance there is a nearby network, which SSID cannot be decoded, the method call will throw an exception.