Hotplug networking with ifplugd


Author: Michael Oliveri

I can’t count the times I’ve set up my laptop, only to forget to plug in the network cable. If I do this on my Slackware laptop, I have to open a terminal and configure the interface manually with the ifconfig and route commands. Every time, I think, “Gee, it would be nice if Slackware took care of this automatically.” If that sounds familiar to you, I have good news.

The ifplugd
interface plug daemon, written by Lennart Poettering and released under the GPL, is
just the tool to handle this situation. ifplugd listens on a specified interface for a new or lost network connection and issues commands based on either event.

If you’re running Mandrake or some other distros, there’s a good chance you’ve already got ifplugd on your system. If not, it’s a simple matter to install it yourself. Installation requires two packages: libdaemon, also written by Poettering, and ifplugd itself. Both are available as source code, though binaries are available for some distributions.

To install each package from source, extract the tarball and execute the usual commands:

tar -zxvf package
cd packagedir
make install
(as root)

Install libdaemon first or you will get dependency errors while compiling ifplugd. If you still receive errors related to libdaemon when configuring infplugd, try running ./configure with the following argument:

./configure PKG_CONFIG_PATH="/path/to/pkgconfig"

On Slackware, my pkgconfig path is /usr/local/lib/pkgconfig.

ifplugd comes with a script to initialize the daemon on boot. The script is installed as /etc/rc.d/ifplugd, though it will be up to you to change your startup configuration to execute ifplugd. This procedure varies by distro. ifplugd’s configuration file and action scripts are installed to /usr/local/etc/ifplugd.

Before starting ifplugd for the first time, open ifplugd.conf in a text editor. There are two lines to configure: INTERFACES and ARGS. The former is a list of interfaces ifplugd will handle. eth0 is listed by default, but this can be changed to an alternate interface or a list of multiple interfaces.

The ARGS line is a little more complex. By default, it appears as:

ARGS="-fwI -u0 -d10"

This means it will assume the interface is down if detection fails (-f), wait for forks (-w), and ignore non-zero script return values (-I). It will execute the action script immediately upon receipt of an up status (-u0) and wait 10 seconds after a connection is lost before issuing the down command (-d10). For a full listing of available arguments, run the command ifplugd -h or consult the man page.

Different arguments can be issued to each interface. For example, you may want a wireless interface to behave differently than a cabled interface. An example is included in the configure file.

Next edit the action script, ifplugd.action. ifplugd passes the interface name and status to the script as variables in the form ifplugd.action [interface] [state].

The script comes with two commands to bring an interface up or down: ifup
and ifdown, respectively. These commands are not included in Slackware, however, and I also wanted the script to handle DHCP, as the three primary locations I travel to all run DHCP. Here’s my final script:

set -e

if [ -z "$1" ] || [ -z "$2" ] ; then
echo "Wrong arguments" > /dev/stderr
exit 1

[ "$2" = "up" ] && exec /sbin/dhcpcd -d -t 10 $1

if [ "$2" = "down" ]
/sbin/dhcpcd -k
/sbin/ifconfig $1 down

exit 1

The initial if statement ensures the correct values are received by the script. If the interface is brought up, it executes the DHCP client daemon to configure the interface. If the interface comes down, it kills the DHCP client daemon and brings down the network interface.

You can start the daemon by running /etc/rc.d/ifplugd start. Test your action script by unplugging your cable. Monitor your connection and /var/log/messages. If you receive an error on the command line or in the log, edit your script and try again. You can also test ifplugd.action by running the script by hand if you prefer; for example ./ifplugd.action eth0 down.

The beauty of ifplugd is it really does nothing more than execute a script, so its possibilities are limitless. You can use ifplugd.action to roll a connection over to a redundant interface, reconfigure firewalls for different connections, start and stop network
services, and more.

In short, if you can script it, ifplugd can do it, making it a handy and flexible tool for administrators and desktop users alike.

Michael Oliveri is the systems administrator for a rural Illinois ISP and writes both fiction and non-fiction. His debut horror novel, Deadliest of the Species, won the Bram Stoker Award in 2001.