When you generate a new config with wpa_passphrase
, put it in a different spot than the last to make things easier. So for example, your home wifi could be in /etc/wpa_supplicant/home.conf
and your work wifi could be in /etc/wpa_supplicant/work.conf
.
Then when you need to connect to your home wifi you just run
killall wpa_supplicant # With root privileges
wpa_supplicant -B -i wlan0 -c /etc/wpa_supplicant/home.conf
And when you need to connect to your work wifi you run
killall wpa_supplicant # With root privileges
wpa_supplicant -B -i wlan0 -c /etc/wpa_supplicant/work.conf
Rince and repeat for any new networks you want to use. If you don't want to keep a network, like a starbucks wifi on a roadtrip, just save it to a conf that you plan on overwriting or deleting like /etc/wpa_supplicant/temp.conf
.
AFAIK, you never have to rerun dhcpcd. I have dhcpcd as a startup process and whenever I switch wifis I never need to touch it.
Edit: You also don't need to run this as a python script. You can do it in the shell. If you need to write a script that quickly changes the wifi, I would recommend you use a shell script like the following for example.
#!/bin/sh
[ -z "$1" ] && exit 1
[ ! -f "/etc/wpa_supplicant/${1}.conf" ] && echo "$1 is not a wpa_supplicant config file" && exit 1
sudo killall wpa_supplicant
sudo wpa_supplicant -B -i wlan0 -c "/etc/wpa_supplicant/${1}.conf"
Run like
wifichange home
or
wifichange work
The [ -z "$1" ]
section is saying to quit if you didn't input anything. (like)
wifichange
And the [ ! -f ...]
section is saying to quit if you didn't input the name of a real config file.
Now I tested the script.