Tuesday 21 August 2018

Bash wifi network switcher script for Raspberry Pi

I had a situation where I had a Raspberry Pi 3B+ in a location where there were two usable wifi networks in range, but neither were particularly reliable in terms of Internet access. The wifi networks themselves would stay up, but the ADSL connections used by the  routers would sometimes drop and stay down for a while (in the case of one, until someone rebooted it).

So I wanted the Pi to switch from one network to the other in the event of losing Internet connectivity.

So I wrote a bash script that can be run periodically by cron.

There are a number of ways to achieve this,  including using additional packages like nmcli, but I wanted something that should just work on a plain version of Raspbian.

I also decided to test it on older Pis without built-in wifi and using a doingle instead. I found that a few tweaks were required to get the script to work reliably. 

I also encountered one situation where the Pi under test ended up not connected to any wifi network at all. Annoyingly I have not been able to reproduce that outcome to identify what went wrong.

The script is set to switch between two networks, SSID1 and SSID1 - obviously you'll need to change these names to reflect your own environment. 

To use the script you'll need two different copies of your wpa_supplicant file - which contains the network name and pre-shared key (password) required to authenticate - , one for each SSID.  Give them sensible names like  wpa_supplicant_1.conf and  wpa_supplicant_2.conf.

The script logs to a file wifi.log - this can be useful for debugging. It also echoes to the terminal too, to make it easier to see what's going on when testing. If you don't want any of this, just remove or comment out the 'echo' lines. 

There's no reason that this shouldn't work on other Debian distros or more widely on Linux, but I have not tested it.

The script needs to be run on a Pi with sudo. If you want to schedule using cron, then type 

sudo crontab -e

and then add a line to run as frequently as you like. For example:

0 */2 * * * /home/pi/wifitest.sh

Don't forget to make the file executable

chmod +x /home/pi/wifitest.sh

1 comment:

  1. ซุปเปอร์ สล็อต สล็อตเป็นเกมที่มีความสนุกสนานและตื่นเต้นที่ไม่ต้องการทักทาย pg เนื่องจากมันไม่ต้องการทักทายทางการเดิมพันในสล็อต และไม่ต้องการมีกลยุทธ์เฉพาะเพื่อชนะในเกม