2011
04.02

I use NetworkManager to connect to my wireless network and wanted a way to map the drives automagically when I connect.  However, as I connect to a number of wireless access points I didn’t want to send my credentials trying to find my home server out each time I connect at the coffee shop.

Enter the NetworkManager dispatcher.  Scripts in /etc/NetworkManager/dispatcher.d/  are executed when the network manager connection is made.  I created the following file as “99mountmydrives” and chmodded it with execute permissions.

See the script below:

#!/bin/sh

IF=$1
STATUS=$2
USER=myusername
SERVERIP=192.168.1.200
SERVERMAC=00:aa:bb:cc:dd:ee

echo "running 99mountmydrives as `whoami`;" >> /tmp/dispatcher-log
echo "int=$1 and status=$2" >> /tmp/dispatcher-log

wait_for_process() {
    PNAME=$1
    PID=`/usr/bin/pgrep $PNAME`
    while [ -z "$PID" ]; do
        echo "waiting." >> /tmp/dispatcher-log
          sleep 3;
          PID=`/usr/bin/pgrep $PNAME`
    done
}

mount_drives() {
    echo "waiting for nm-applet" >> /tmp/dispatcher-log
    wait_for_process nm-applet
    echo "mounting drives" >> /tmp/dispatcher-log
    /home/mvoorberg/automountmyth.sh
}

if [ "$IF" = "wlan0" ] && [ "$STATUS" = "up" ]; then
    #WIFI Subnet at home

    test=`ping -c 3 $SERVERIP >/dev/null;arp -a $SERVERIP`
    echo "$test" >> /tmp/dispatcher-log

    if expr "$test" : ".*$SERVERMAC" 2> /dev/null ; then
        mount_drives
        exit $?
    fi
fi