0

Sometimes Ubuntu loses its connection (for example the router goes down), and I want to make sure that it reconnects to the right network. I don't want to just check that there is an internet connection because the modem might be down. I would use this in a script called by cron.

In pseudo code:

if not connected to "wifiname" then 
  connect to "wifiname". 

It should be simple, but most solutions want to check if connected to the Internet, but not a certain network.

2
  • Why not simply delete the known connections in Network Manager and leave only the one you want ? It connects only to whats remembered like on android. I dont see a point for a script. Can you explain ? Feb 22, 2017 at 8:35
  • It's hard to explain but I have 3 wifi networks and sometimes want to stay connected to a certain one. For example I have a VPN service connected to one. Another gives better speed for some reason. Even when the VPN is off. I want to apologize to the mods who had to edit my post. I was on my phone and in a rush.
    – JClar
    Feb 22, 2017 at 16:10

1 Answer 1

1

Find the name of your configured connection with:

$ ls /etc/NetWorkManager/system-connections/

Replace the My Choice Network with the name of the network you want to connect to.

Run the script. If it's connected to that network, it'll do nothing. If it's not connected to that network it connect to it.

#!/bin/bash

wifiname="My Choice Network"
connectedname="$(nmcli -f active,ssid dev wifi|egrep "yes"|awk '{$1="";print}'|sed "s/^[ \t]*//")"

echo "Connedted: [$connectedname]"
echo "wifiname:  [$wifiname]"

if [[ ! "$connectedname" == "$wifiname" ]]; then
        nmcli connection up id "$wifiname"
fi

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.