================================================================================================================================================================

Linux Laptop: Wifi dependent profiles

bash linux network-manager wifi

A notebook as mobile device may should behave differently depending on the location. Examples:

Evaluating the ssid (name) of the currently connected Wi-Fi on connection change is a cheap solution. This can be done by using a hook of the network manager.

  1. Create file /etc/NetworkManager/dispatcher.d/90-wifi-profiles (replace 3x yourname)
#!/usr/bin/env bash

interface=$1
event=$2

# only listen to connect/disconnect events
[[ "$event" == "connectivity-change" ]] || exit 0   

# check user is logged in
who | awk '{print $1}' | grep -q '^yourname$' || exit 0

sudo -u yourname /home/yourname/.wifi-profile >> /var/log/wifi-profiles 2>&1

and run

$ sudo chown root:root /etc/NetworkManager/dispatcher.d/90-wifi-profiles
$ sudo chmod o+x /etc/NetworkManager/dispatcher.d/90-wifi-profiles
  1. Create ~/.wifi-profile
#!/usr/bin/env bash

# user specific env vars need to be set manually!
export XDG_RUNTIME_DIR=/run/user/1000
export DISPLAY=:0

function soundOff() {
  amixer -D pulse sset Master mute
}

function soundOn() {
  amixer -D pulse sset Master 40%
  amixer -D pulse sset Master unmute
}

function askConnectVpn() {
  zenity --question --title "Foreign Wifi" --text "Connect to home VPN?" && nmcli con up id yourvpn
}

SSID="$(iwgetid -r)"

if [[ "$SSID" = 'home_network1' || "$SSID" = 'home_network2' ]]; then  # at home
  soundOn  # use carefully, might not be wanted
  # run backup
elif [[ "$SSID" = 'work_network1' || "$SSID" = 'work_network2' ]]; then  # at work
  soundOff
elif [[ "$SSID" = '' ]]; then  # not connected
  soundOff
else  # foreign network
  soundOff
  askConnectVpn
fi

and run

$ chmod o+x ~/.wifi-profile
  1. Test
$ sudo /etc/NetworkManager/dispatcher.d/90-wifi-profiles wlan0 connectivity-change
$ cat /var/log/wifi-profiles