Have you been wondering if and how it is possible do do things automatically, based on your current network connection?

For instance when establishing a connection you could check your mail, start a download tool, etc. You could even stop the download when there's no connection left... Another appliance would be to mount your home server as soon as your connected to your home network and unmount it when you disconnect from your network! (actually i use this to solve a system freeze during shutdown (and the fsck the next boot...) when i forget to disconnect before shutting down my laptop)

Basics (works with NetworkManager as well as the Standard network setup)

  1. You must put your scripts into 1 of the following 2 folders to get them executed on the appropriate event:
    (normally you can put the "onConnection" script in "if-up.d" and the "onDissconnection"-script in "if-down.d")

    • /etc/network/if-up.d/

    • /etc/network/if-down.d/

NOTE: NetworkManager does not execute scripts placed in "/etc/network/if-pre-up" or "/etc/network/if-post-down".

If they don't exist you must create them as superuser. for example :

sudo mkdir /etc/network/if-down.d/
  1. Write your script and save it.
  2. Now you can copy it to one of the folders above and mark the script executale. must be superuser!

sudo cp <pathtoyoursavedfile>/<file> /etc/network/<folderyouselect>
sudo chown root:root /etc/network/<folderyouselect>/<file>
sudo chmod 755 /etc/network/<folderyouselect>/<file>

If you've configured your network with the Standart Network Administration Tool or the shell

This only applies when you have configured your network with the standard tool shipped with Ubuntu 6.10 (or older) or configure it from the shell.

  1. do the step described for network manager
  2. Edit the text configuration file /etc/network/interfaces and add one of the following directive pre-up , post-up , pre-down , post-down to the appropriate network connection, for example :

auto eth0
iface eth0 inet dhcp
name WiFi
post-up /etc/network/if-up.d/myscript.sh
  1. restart your network

/etc/init.d/networking restart


If you want to do something only when connecting to a certain network you need your scripts to check if the network connection matches. To do this you could check the SSID (the Network Name that Identifies a Network). Here is an example of how this can be achieved :

if [ `iwlist eth0 scanning | grep YourNetworkSSID` ]
mount // /mountpoint -t cifs -o iocharset=utf8,gid=1000,uid=1000,credentials=/root/.smbcredentials,dmask=770,fmask=770

On a wired LAN you could ping a specific Server as follows (look in /etc/hostname, /etc/hosts, /etc/dhcp3/dhcpd.conf for how to configure this) :

if [ `ping -c 1 server.local | grep "1 packets transmitted"` ]


written by Bernstein with help from IntuitiveNipple

OnNetworkConnectionRunScript (last edited 2009-03-02 15:16:18 by joe-rl-duncan)