Is there a way to run a script every time I connect to a specifc wireless network?

There is a specific wireless network I connect to that requires me to use vpnc to access the internet. If I make a script to do this, is there a way to set it up so every time I connect to this network, the script is run? To be clear, I don't want this script run with most wireless connections, just one specific one.


Solution 1:

You can use upstart for this. Upstart is able to catch the event of connecting to a wireless network (or a network in general) and use that as the prerequisite for running your script.

Upstart is already used by lots of scripts in Ubuntu and will eventually replace all the System V init scripts at bootup time.

Solution 2:

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).

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).