wpa_supplicant - how to switch to different network?

6.9k Views Asked by At

What I need: Connect to different wifi network on archlinux by calling python script.

What I am doing: Executing the following statements from python:

wpa_passphrase "MySSID" "MyPass"> /etc/wpa_supplicant/profile.conf
wpa_supplicant -B -i wlan0 -c /etc/wpa_supplicant/profile.conf
dhcpd wlan0

It works only for the first attempt. When it is executed the second time, it says dhcpd is already on. I dont know how to switch to another network.

I have also tried wpa_cli and again, dont know how to switch to another network.

Please suggest some fix or alternatives (uncomplicated)

3

There are 3 best solutions below

1
On

Edit /etc/wpa_supplicant.conf

nano /etc/wpa_supplicant.conf

Complete the file to make it look like that (replacing wifi_name and wifi_key by their real values of course).

network={
         ssid="wifi_name1"
         psk="wifi_key1"
}

and

network={
         ssid="wifi_name2"
         psk="wifi_key2"
}

Then save and exit

The wifi network is now configured, we must now tell that we want to connect to it using this configuration file.

wpa_supplicant -B -i wlan0 -c <(wpa_passphrase MYSSID passphrase)

If your interface isn't named wlan0 then replace wlan0 by the real name of your interface. We must now request an IP adress.

dhclient wlan0

If everything gone well you now see several lines containing some IP addresses and the command ping should work.

0
On

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.

0
On

Your concrete Problem is that you start wpa_supplicant and dhcp instead of re-starting them. I have a script that reads

#shutdown dhc
dhclient -r
#shutdown wpa_supplicant
killall wpa_supplicant
#down interface
ifdown --force wlan0
sleep 1
#your wpa startup here:
wpa_supplicant -t -fYOUR_LOG_FILE -cYOUR_wpa_supplicant.conf -B -iwlan0
sleep 1
#restart dhc
dhclient -nw

I guess you can do this a little more nicely by configuring your /etc/network/interfaces appropriately.

Btw. in principle, it should not be necessary to restart the dhc at all. After some while it should realize that it needs to fetch a new IP, but for me this takes to long. ;)