Switching Between Networks With wpa_supplicant
May 16, 2021, 1:34 p.m. by Avery Uslaner
My setup manages its Wi-Fi connection with wpa_supplicant but doesn't have a desktop environment so it's kind of annoying when I want to switch VLANs to manage my IoT devices.
The two networks I'm switching between are defined in /etc/wpa_supplicant/wpa_supplicant-wlp2s0.conf and their priority set by the priority variable. To make switching easier, I've made a script to flip the priorities and restart the wpa_supplicant systemd service.
#!/usr/bin/python3 import subprocess with open('/etc/wpa_supplicant/wpa_supplicant-wlp2s0.conf', 'r') as f: filedata = f.readlines() for i, line in enumerate(filedata): if 'priority=1' in line: filedata[i] = line.replace('priority=1', 'priority=0') elif 'priority=0' in line: filedata[i] = line.replace('priority=0', 'priority=1') with open('/etc/wpa_supplicant/wpa_supplicant-wlp2s0.conf', 'w') as f: f.writelines(filedata) subprocess.run(['systemctl', 'daemon-reload'], check=True) subprocess.run(['systemctl', 'restart', 'wpa_supplicant@wlp2s0'], check=True)