Preferred wireless ESSID with Network Manager

I’ve been looking for a way for Network Manager to automatically connect to a specific wireless network in Ubuntu 12.04, and finally found a way to do it. Network Manager connects to the last available ESSID by default, but sometimes you may want to force it to connect to a different one. It’s actually quite simple to do, once you know how Network Manager works.

When the wireless card is taken up or down (like when you resume from suspend or start your computer), the scripts in /etc/NetworkManager/dispatcher.d/ are run. It sends two parameters, the interface and the status (e.g. up or down), and the UUID of the connection is available as the environment variable $CONNECTION_UUID. You can find the UUID by checking the files corresponding to your connection name under /etc/NetworkManager/system-connections/.

This is a script i created which forces Network Manager to reconnect to gaukvn23 if it tries to connect to two other networks. Remember to make it executable with chmod. I named it /etc/NetworkManager/dispatcher.d/99PreferGaukvn23 so it will be called after any other script.

#!/bin/bash
myessid=gaukvn23
INTERFACE=$1
STATUS=$2
if [ "$INTERFACE" == "wlan0" -a "$STATUS" == "up" -a \( "$CONNECTION_UUID" == "1c4a6642-630d-4488-869a-0e883d37d511" -o "$CONNECTION_UUID" == "6b28428b-a8d2-486c-a9ce-03d85d3a3e17" \) ]; then
  #check if gaukvn23 is available
  iwlist wlan0 scanning | grep ESSID:\"gaukvn23\"
  if [ $? = "0" ]; then
    nmcli con up id $myessid
  fi
fi

You can do all sorts of other awesome things, like mounting remote folders over sshfs, automatically connect to VPNs and starting/stopping services. See some more examples over at the ArchWiki.

Publisert 13. jun, 2012

Lagret i Teknologi og merket bash, force, hack, reconnect, scripting

spacer Ingen svar

Skriv kommentar | Tilbaketråkk

Legg igjen et svar
Dropp svar

  • Piano Hero »
  • « Elektronisk arkiv med kvitteringer
gipoco.com is neither affiliated with the authors of this page nor responsible for its contents. This is a safe-cache copy of the original web site.