Changing Wifi network within a container

Hello.
I am working on a project where I am trying to change the wifi connection of my device in the container while it is running. I am able to add a connection and I can see it in my /etc/NetworkManager/system-connections/ however, I am unable to actually use this connection. Using this code:

wifi_dev = ''
for  device  in  self.list_devices():
	if  device.type  ==  2:
		wifi_dev = device.path
connection  = {
'connection': {
'id': f'Wireless-{ssid}',
'uuid': str(uuid.uuid4()),
'type': '802-11-wireless',
'autoconnect': True},
'802-11-wireless': {
'hidden': dbus.Boolean(True),
'mode': 'infrastructure',
'ssid': dbus.ByteArray(bytes(ssid) if  sys.version_info  < (3, 0) else  bytes(ssid, 'utf-8')),},
'802-11-wireless-security': {
'auth-alg': 'open',
'key-mgmt': 'wpa-psk',
'psk': password },
'ipv4': {'method': 'auto'},
'ipv6': {'method': 'auto'}
}
proxy  =  bus.get_object("org.freedesktop.NetworkManager", "/org/freedesktop/NetworkManager/Settings")
settings  =  dbus.Interface(proxy, "org.freedesktop.NetworkManager.Settings")
conn_path  =  settings.AddConnection(connection)
proxy  =  bus.get_object("org.freedesktop.NetworkManager", "/org/freedesktop/NetworkManager")
nm  =  dbus.Interface(proxy, "org.freedesktop.NetworkManager")
acpath = nm.ActivateConnection(conn_path, wifi_dev, '/', timeout=TIMEOUT)
proxy = bus.get_object("org.freedesktop.NetworkManager", acpath)
active_props = dbus.Interface(proxy, "org.freedesktop.DBus.Properties")
state = active_props.Get("org.freedesktop.NetworkManager.Connection.Active", "State")
print(state)

all that happens is that my device goes offline for ~5s and then reboots the container, sending the wifi back to the original network and continues as if that part of my code never happened. I was wondering if someone could give me any insight into a) what is happening and/or b) how I can fix this.

Most of this looks fine to me, I would assume that the connection fails to establish for some reason and potentially an exception (from e.g ActivateConnection) causes the crash. If the wifi connection is the one in use while doing this procedure then it will bring the device offline.

I would recommend manually creating the connection-file on the device and then use nmcli to activate the connection and see if you can narrow it down; If nmcli does not provide enough information Iā€™d also recommend using journalctl and follow the NetworkManager service logs.

1 Like

I agree with @Simontaga @MJ6623

You can find this video where we recorded this https://youtu.be/e5-r_9ggHqI?t=1485

Thank you guys so much! This is very helpful!

1 Like