OnNetworkConnectionRunScript

Differences between revisions 1 and 2
Revision 1 as of 2007-01-22 01:31:21
Size: 2133
Editor: 80-218-242-86
Comment:
Revision 2 as of 2007-01-22 14:31:46
Size: 2871
Editor: 80-218-242-86
Comment:
Deletions are marked like this. Additions are marked like this.
Line 7: Line 7:
= Basics (applies for NetworkManager as well as the Standart network config) = = Basics (works with NetworkManager as well as the Standart network setup) =
Line 25: Line 25:
sudo chown root:root /etc/network/<folderyouselect>/<file>
Line 28: Line 29:
= When using : Standart Network Administration Tool / shell = === If you've configured your network with the Standart Network Administration Tool or the shell ===
Line 44: Line 45:

= Advanced =

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 :
{{{
#! /bin/sh
if [ `iwlist eth0 scanning | grep BernSteinNet` ]
then
mount //192.168.0.2/dataserver /media/dataserver -t cifs -o iocharset=utf8,gid=1000,uid=1000,credentials=/root/.smbcredentials,dmask=770,fmask=770
fi
}}}

=== About ===
written by Bernstein with help from [http://www.ubuntuforums.org/showthread.php?t=343284 IntuitiveNipple]

Introduction

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 homeserver as soon as your connected to your homenetwork 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 Standart network setup)

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

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

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

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

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

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 standart 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

Advanced

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 BernSteinNet` ]
then
mount //192.168.0.2/dataserver /media/dataserver -t cifs -o iocharset=utf8,gid=1000,uid=1000,credentials=/root/.smbcredentials,dmask=770,fmask=770
fi

About

written by Bernstein with help from [http://www.ubuntuforums.org/showthread.php?t=343284 IntuitiveNipple]

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