3

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 ?

Robert
  • 177

1 Answers1

2

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
anonymous2
  • 4,298
ob2
  • 3,553
  • this is perfect, after some fixes in the code it works like a charm. As soon they approve my edits, I'll mark the reply as solved – Robert Nov 05 '20 at 17:04
  • Everything works fine until I invoke /home/robert/back-wifi-main from the terminal but not from the cron, the script looks not to be executed. Do you kindly have some debug for cron? Thank you. Script is executable and owned by robert.robert with 777 and the cron is */1 * * * * /home/robert/back-wifi-main and a check with systemctl status cron shows that the script is in the list every minute with (robert) CMD (/home/robert/back-wifi-main) – Robert Nov 06 '20 at 18:33
  • /1 is probably useless, you can just place ` * * * * /home/robert/back-wiki-mainso your script will be run every minute. Be careful that may drain a bit your battery while you are connected on your Emerg wifi. If it's still doesn't work, you should check the logs withjournalctl -f` if there is any error displayed. – ob2 Nov 07 '20 at 21:53
  • It is an excellent answer, for me, it worked after a rectification. Which is: Reconnect to your wifi nmcli d wifi connect Main <password>. In first attempt, it is not worked and give argument error, I run it manually and found that is not required here as it is already saved, after removing password it's working fine. Regards – kami Dec 18 '22 at 08:22