Last Update: March 21, 2015

WiFi Radar HOWTO

Connect and Disconnect Scripts

by Sean Robinson

There are two points in each of the connecting and disconnecting phases in which custom scripts can be executed through the shell. These are (under normal circumstances):

  1. connect pre-script
  2. connect post-script
  3. disconnect pre-scipt
  4. disconnect post-script

Scripts are executed in an environment containing several environment variables. Those variables are detailed in a separate HOWTO.

A sample generic script to use for any of the four scripts:

#!/bin/bash # # rc.net_services - start/stop network daemons that are only needed while connected to a network # # ACTION=$1 shift case "$ACTION" in start) ;; stop) ;; *) echo "usage: rc.net_services start|stop " exit ;; esac # Start/Stop the Network Time Protocol daemon: function ntpd { test -x /etc/rc.d/rc.ntpd && /etc/rc.d/rc.ntpd $1 } # Privoxy, the privacy web proxy function privoxy { test -x /etc/rc.d/rc.privoxy && /etc/rc.d/rc.privoxy $1 } # Tor, the Internet anonymizer function tor { test -x /etc/rc.d/rc.tor && /etc/rc.d/rc.tor $1 } function hostname { case "$1" in stop) # Set the hostname. if [ ! -r /etc/HOSTNAME ]; then # fall back on this old default: echo "" > /etc/HOSTNAME fi /bin/hostname $(cat /etc/HOSTNAME | cut -f1 -d .) /bin/domainname $(cat /etc/HOSTNAME | cut -f2- -d .) ;; *) ;; esac } # Start/Stop printers in the spooling system. function print { case "$1" in stop) # enable all printers /usr/sbin/cupsenable `/usr/bin/lpstat -a | cut -d' ' -f1` ;; start) # disable all printers /usr/sbin/cupsdisable `/usr/bin/lpstat -a | cut -d' ' -f1` ;; esac } for service in $@; do case $service in ntpd) ntpd $ACTION ;; privoxy) privoxy $ACTION ;; tor) tor $ACTION ;; hostname) hostname $ACTION ;; print) print $ACTION ;; *) echo "$ACTION, but invalid service: $service" ;; esac done

I use this one script differently depending on the network, but the way I commonly use it for public hotspots is:

Connection After: /etc/rc.d/rc.net_services start tor privoxy

Disconnection Before: /etc/rc.d/rc.net_services stop tor privoxy

Disconnection After: /etc/rc.d/rc.net_services stop hostname

So, this starts and stops TOR and Privoxy while the network is present and resets the host name after disconnecting (because DHCP can change the host name on configuration).