Switching Between Networks With wpa_supplicant

Tags: Wi-Fi wpa_supplicant arch


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.


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:

subprocess.run(['systemctl', 'daemon-reload'], check=True)
subprocess.run(['systemctl', 'restart', 'wpa_supplicant@wlp2s0'], check=True)