hyndrix
Posts: 2
Joined: Thu Sep 22, 2016 10:49 am

Switching between known Wifi networks

Thu Sep 22, 2016 10:54 am

Hi,
I already posted this on stackoverflow but without success:

I want to switch between known Wifi networks using the terminal/shell scripts without manual intervention. The Wifi networks are registered using the built-in Raspbian Wifi managing tool.

The wicd-cli tool (apt-get install wicd-cli) seems to be perfect for this:

Code: Select all

wicd-cli --wireless -c "myssid" "mypassword"
The command executes successfully with a Done! message but the current Wifi connection is disconnected and the new one to myssid is not built up.

I also tried to connman tool but it completely breaks Wifi until the tool is uninstalled.

How can I switch between Wifi networks?

Regards,

User avatar
lmarmisa
Posts: 1307
Joined: Thu Feb 14, 2013 2:22 am
Location: Jávea, Spain

Re: Switching between known Wifi networks

Sat Sep 24, 2016 10:15 am

I recommend this procedure.

Define two (or more) wifi profiles for different wifi networks. I will store those profile files in the directory /etc/wpa_supplicant. For example:

File /etc/wpa_supplicant/wifi1

Code: Select all

country=ES (use your country code here!)  
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1

network={
	ssid="SSID_WIFI1_HERE"
	psk="passphrasewifi1here"
}
File /etc/wpa_supplicant/wifi2

Code: Select all

country=ES (use your country code here!)  
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1

network={
	ssid="SSID_WIFI2_HERE"
	psk="passphrasewifi2here"
}
Then you have to create a script for switching between networks:

Code: Select all

nano switchwifi

Code: Select all

#!/bin/bash

cp /etc/wpa_supplicant/$1 /etc/wpa_supplicant/wpa_supplicant.conf

dhclient -r wlan0
ifdown wlan0
ifup wlan0
dhclient -v wlan0

echo
iwconfig wlan0
ifconfig wlan0
Give exec permission to your script file:

Code: Select all

chmod +x switchwifi
The script must be run with root privileges:

Code: Select all

sudo ./switchwifi wifi1
sudo ./switchwifi wifi2

Return to “Advanced users”