May 052011
 

Did you ever wish you could start certain scripts only when you are bringing the network up and then stop them when bring the network down?

For the longest time, I was using wicd to manage network connections.  At some point and time I decided to take a look at NetworkManager.

Using network manager, you can use the dispatcher to run scripts based on network connection, runlevel, etc.

This is how I got it working in gentoo linux and a few example scripts.

The following are the specific packages and versions I have installed:

net-misc/cnetworkmanager
      Latest version available: 0.21.1
      Latest version installed: 0.21.1
      Size of files: 28 kB
      Homepage:      http://vidner.net/martin/software/cnetworkmanager/
      Description:   Command line interface for NetworkManager.
      License:       GPL-2
 
*  net-misc/networkmanager
      Latest version available: 0.8.2-r10
      Latest version installed: 0.8.2-r10
      Size of files: 1,591 kB
      Homepage:      http://www.gnome.org/projects/NetworkManager/
      Description:   Network configuration and management in an easy way. Desktop environment independent.
      License:       GPL-2
 
*  net-misc/networkmanager-openvpn
      Latest version available: 0.8.2-r1
      Latest version installed: 0.8.2-r1
      Size of files: 394 kB
      Homepage:      http://www.gnome.org/projects/NetworkManager/
      Description:   NetworkManager OpenVPN plugin.
      License:       GPL-2
 
*  net-misc/networkmanager-pptp
      Latest version available: 0.8.2
      Latest version installed: 0.8.2
      Size of files: 375 kB
      Homepage:      http://www.gnome.org/projects/NetworkManager/
      Description:   NetworkManager PPTP plugin
      License:       GPL-2
 
*  net-misc/networkmanager-vpnc
      Latest version available: 0.8.2
      Latest version installed: 0.8.2
      Size of files: 367 kB
      Homepage:      http://www.gnome.org/projects/NetworkManager/
      Description:   NetworkManager VPNC plugin
      License:       GPL-2

Along with the use flags I have compiled with:

# emerge -pv networkmanager
 
These are the packages that would be merged, in order:
 
Calculating dependencies... done!
[ebuild   R   ] net-misc/networkmanager-0.8.2-r10  USE="avahi bluetooth connection-sharing dhcpcd gnutls nss resolvconf -dhclient -doc" 0 kB

Once that is done, disable all net devices from loading through udev.  You do that by simply adding this line to /etc/conf.d/rc

RC_PLUG_SERVICES="!net.*"

The next part you have to really think about.  Any net related service except net.lo really needs to be under the control of network manager.  So, what I did was grepped for net in /etc/init.d/* and made a list.

# grep "use net" /etc/init.d/*

And the list was:

/etc/init.d/avahi-daemon:    use net
/etc/init.d/avahi-dnsconfd:    use net
/etc/init.d/clamd:    use net
/etc/init.d/cupsd:    use net
/etc/init.d/lisa:    use net
/etc/init.d/mrtg:    # use net-snmpd
/etc/init.d/mysql:    use net.lo
/etc/init.d/netperf:    use net
/etc/init.d/ntpd:    use net dns logger
/etc/init.d/rpcbind:    use net
/etc/init.d/rsyncd:    use net
/etc/init.d/staticroute:    use network
/etc/init.d/xinetd:    use net

So then I checked what I currently had listed in my default and battery runlevels, and removed anything that was in the list above that required net from it.  This was mysql, iptables, cupsd, ntpd, sshd

I ended up with only this:

rc-config list | grep default
sysstat                   default

Then everything I removed, I created a script to be started by networkmanager.

Here are the scripts I placed in /etc/NetworkManager/dispatcher.d

echo "ran `date +%Y%m%d`" >> /tmp/nwm-firewall
 
/etc/init.d/iptables status | grep -q "started"
started=$?
 
echo $2 >> /tmp/nwm-firewall
echo $started >> /tmp/nwm-firewall
 
if [[ "$2" == "up" ]] ; then
        if [[ "$started" != "0" ]] ; then
                rc-config start iptables
        fi
else
        rc-config stop iptables
fi
49-sshd   
echo "ran `date +%Y%m%d`" >> /tmp/nwm-sshd
 
/etc/init.d/sshd status | grep -q "started"
started=$?
 
if [[ "$2" == "up" ]] ; then
        if [[ "$started" != "0" ]] ; then
                rc-config start sshd
        fi
else
        rc-config stop sshd
fi
50-ntpd   
echo "ran `date +%Y%m%d`" >> /tmp/nwm-ntpd
 
/etc/init.d/ntpd status | grep -q "started"
started=$?
 
if [[ "$2" == "up" ]] ; then
        if [[ "$started" != "0" ]] ; then
                rc-config start ntpd
        fi
else
        rc-config stop ntpd
fi
51-mysql   
echo "ran `date +%Y%m%d`" >> /tmp/nwm-mysql
 
/etc/init.d/mysql status | grep -q "started"
started=$?
 
if [[ "$2" == "up" ]] ; then
        if [[ "$started" != "0" ]] ; then
                rc-config start mysql
        fi
else
        rc-config stop mysql
fi
52-cups   
echo "ran `date +%Y%m%d`" >> /tmp/nwm-cupsd
 
/etc/init.d/cupsd status | grep -q "started"
started=$?
 
if [[ "$2" == "up" ]] ; then
        if [[ "$started" != "0" ]] ; then
                rc-config start cupsd
        fi
else
        rc-config stop cupsd
fi

One thing you will notice in the above, there is no shebang line ( #!/bin/bash ) because it is not needed.  The files are owned root:root and have 700 permissions.

dispatcher.d # ls -la
total 20
drwxr-xr-x 2 root root 120 May  5 18:28 .
drwxr-xr-x 5 root root  90 Apr 27 17:00 ..
-rwx------ 1 root root 336 May  5 18:26 48-firewall
-rwx------ 1 root root 255 May  5 18:26 49-sshd
-rwx------ 1 root root 255 May  5 18:27 50-ntpd
-rwx------ 1 root root 259 May  5 18:27 51-mysql
-rwx------ 1 root root 259 May  5 18:28 52-cupsd

So the numbering of the files affects the order they are started or shutdown, lower numbers get started first.

If you look inside /var/log/messages and restart /etc/init.d/NetworkManager you can see any error logs.  Also, in my examples above, I echo some stuff to files inside of /tmp for diagnostics, mostly just that the script was ran and the output of the init.d status check.

This site uses Akismet to reduce spam. Learn how your comment data is processed.