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
  • 8
  • Whats the OS in the Question? – PRATAP Nov 05 '20 at 14:59
  • 1
    I'm using ubuntu 20.04 – Robert Nov 05 '20 at 15:27
  • 1
    Does this answer your question? [How to manage available wireless network priority?](https://askubuntu.com/questions/165679/how-to-manage-available-wireless-network-priority) – PRATAP Nov 05 '20 at 15:36
  • @UnKNOWn already seen that reply BEFORE to write this question, the answer is "no because wifi-radar is not yet available for ubuntu 20.04" – Robert Nov 05 '20 at 17:15
  • https://askubuntu.com/a/722010/739431 did you try this one? – PRATAP Nov 05 '20 at 17:22
  • though I'm not sure that with the two SSIDs available, anything will make the switch – Robert Nov 05 '20 at 17:23
  • Ok.. When you manulaay choose 'emerg'? That time 'Main' is turned off or out of reach? – PRATAP Nov 05 '20 at 17:26
  • more often it is turned-off-like , though the script allows more interesting options, since you can test internet connectivity and manage the behaviors. In fact sometime Main has powerful wifi signal but no internet connection and in the script I can test internet connection too and switch back if internet is not reachable all automatically – Robert Nov 05 '20 at 17:29
  • Yes..thats a good script.. But entering password is little security risk IMHO – PRATAP Nov 05 '20 at 17:30
  • 1
    No need of password since the SSID has the password already saved, in fact it is optional it works perfectly either without password – Robert Nov 05 '20 at 17:31

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,268
  • 7
  • 33
  • 61
ob2
  • 3,517
  • 17
  • 16
  • 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-main` so 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 with `journalctl -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 `. 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