WiFi how to automatically connect back to a preferred SSID when available

Since wifi-radar is not yet available on ubuntu 20.04, I go to explain my need

I have two WiFi networks, the main WiFi has SSID Main

the other is a sort of backup and it has a low signal and poor performance, let’s call its SSID Emerg

Sometime the Main SSID becomes unavailable I suppose for some sort of interference, so I manually switch to connect to the Emerg WiFi

When Main returns available … obviously the WiFi remains connected to the Emerg

Well here it comes the question

Is it possible to setup the WiFi in such a way that when Main SSID returns available, the WiFi goes automatically back to connect to Main ?

Asked By: Robert

||

Create a new script with :

sudo nano /usr/local/bin/back-wifi-main

Paste the following content (replace by your Wifi password)

#!/bin/bash

## Get the current Wifi
current=$(iwconfig 2>/dev/null | grep ESSID | cut -f 2 -d ")

if [ $current == "Emerg" ] ; then
    # Check if Wifi is back
    if nmcli d wifi list | grep '^ ' | grep "Main" ; then
       # Reconnect to your wifi
        nmcli d wifi connect Main <password>
    fi
fi

Make it executable

sudo chmod +x /usr/local/bin/back-wifi-main

Finally add this script into a crontab

sudo crontab -e

and paste the following content to check every 5 minutes

*/5 * * * * /usr/local/bin/back-wifi-main
Answered By: ob2
Categories: Answers Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.