Apologies for my previous answer which was the way I would have done it years ago. Seems things have changed.
It turns out that Network Manager runs ALL of the scripts in the /etc/NetworkManager/dispatcher.d/
directory (those owned by root, that are executable, that are not readable by other users, and not setuid), when a connection changes (up, down, preup, predown).
Environment variables are set and passed to this script by Network manager. You'll be interested in the CONNECTION_UUID environment variable (contains a unique string).
So, to solve your problem (execute a script when a particular wireless network is connected to):
1) find out the uuid of the wireless connection you're interested in (by looking inside the appropriate connection file in the /etc/NetworkManager/system-connections/
directory).
2) write a bash (or perl, or python, or whatever) script that does what you want if the environment variable CONNECTION_UUID is a match to the uuid of the wireless network in (1) above.
3) put this script into /etc/NetworkManager/dispatcher.d/
and set the owner and permissions appropriately.
further reading: man networkmanager ( and a litte poking around the scripts in the directories mentioned above).
An example script:
#!/bin/bash
#####################################
# MounterBeast Script
# /etc/NetworkManager/dispatcher.d/02remotemount
# Copyright 2011 Nathan E. Williams
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# Usage:
# This script must be customized for your configuration.
# By default, the script will attempt to mount a CIFS share
# when a specified MAC address is found at the network gateway,
# or over sshfs if the MAC address of the gateway is not the specified MAC.
# e.g. I mount over CIFS to the servers internal IP when at home, and
# over sshfs when away from home.
#
# id gateway mac without physically checking the sticker:
# $ arp -n -a $(ip route show 0.0.0.0/0 | awk '{print $3}') | awk '{print $4}'
#
# Testing:
# up) sudo /etc/NetworkManager/dispatcher.d/02remotemount wlan0 up
# down) sudo /etc/NetworkManager/dispatcher.d/02remotemount wlan0 down
#####################################
#
# Configuration:
#
targetmac='xx:xx:xx:xx:xx:xx'
mount_user='$USER'
mount_pass='pass'
internal_server_name='192.168.1.102'
external_server_name='my.dyndns.com'
share_name="music"
mount_point='/mnt/remote'
ssh_port='22'
#
# Should not need to edit below
#
gateway=$(ip route show 0.0.0.0/0 | awk '{print $3}')
mactest=$(arp -n -a $gateway | awk '{print $4}')
if [[ "$mactest" == "$targetmac" ]]
then
case "$2" in
up)
sleep 5
mount -t cifs -o username=$mount_user,password=$mount_pass //$internal_server_name/$share_name $mount_point
;;
down)
umount -l $mount_point
;;
esac
else
case "$2" in
up)
sleep 5
sshfs -p $ssh_port $external_server_name:$share_name $mount_point
;;
down)
umount -l $mount_point
;;
esac
fi
exit $?
/etc/NetworkManager/dispatcher.d/01ifupdown
executes scripts in/etc/networking/if-*
directories – Peter Chaula Jul 24 '17 at 17:53grep
over the system-connections files. Works nicely. Something likeessid=$(grep -l "uuid=$CONNECTION_UUID" /etc/NetworkManager/system-connections/*)
followed byessid=$(basename $essid)
. – David Faure Dec 09 '19 at 18:39echo $(grep -Z -l "uuid=$CONNECTION_UUID" "/etc/NetworkManager/system-connections/my connection.nmconnection" | xargs -0 -n1 -r basename | wc -l)
– loop Jan 06 '22 at 17:09echo $(grep -Z -l "type=wifi" /etc/NetworkManager/system-connections/* | xargs -0 -n1 -r grep "uuid=$CONNECTION_UUID" | wc -l)
– loop Jan 06 '22 at 17:34