Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I'm working on a linux gadget.

I want it to get DHCP if I plug in the network cable after it has booted up already or if the network cable unplugged and replugged.

One solution is to run a script such as this (which works, btw):

#!/bin/bash

NET_STATUS='different'

while true
do
  NEW_NET_STATUS=`ifconfig | grep RUNNING | grep -v LOOPBACK`
  if [ "${NEW_NET_STATUS}" = "${NET_STATUS}" ]
  then
    echo "no change"
    sleep 1
    continue
  fi
  NET_STATUS=${NEW_NET_STATUS}
  if [ "${NET_STATUS}" ]
  then
    echo "cable plugged in"
  else
    echo "cable unplugged"
  fi
  sleep 1
done

However, I've got a feeling deep down in my little toe that tells me that there's a better way to deal with hotplug events for the ethernet cable.

share|improve this question
add comment

4 Answers

up vote 4 down vote accepted

netplug

netplug is the solution that I went with. ifplugd may work just as well.

Installation

sudo apt-get install netplug

Interface Configuration

cat /etc/netplug/netplugd.conf
eth*

Event Configuration

cat /etc/netplug/netplug
#!/bin/sh
PATH=/usr/bin:/bin:/usr/sbin:/sbin
export PATH

dev="$1"
action="$2"

case "$action" in
in)
    echo "$dev : $action : plugged in" >> /tmp/netplug.log
    ;;
out)
    echo "$dev : $action : unplugged" >> /tmp/netplug.log
    ;;
probe)
    echo "$dev : $action : probed" >> /tmp/netplug.log
    ;;
*)
    echo "$dev : $action : I feel violated" >> /tmp/netplug.log
    exit 1
    ;;
esac

Testing

/etc/init.d/netplug stop
/etc/init.d/netplug start

cat /tmp/netplug.log
eth0 : probe : probed
eth1 : probe : probed
...
eth15 : probe : probed
eth0 : in : plugged in
share|improve this answer
add comment

Using this software of mine for network configuration, it's very easy.

http://code.google.com/p/badvpn/wiki/NCD

The following NCD program will do. It also shows how you can extend the configuration, in this case by running something when the network goes up/down.

process eth0 {
    # Set device.
    var("eth0") dev;

    # Wait for device to appear, set it up, and wait for cable to be plugged in.
    net.backend.waitdevice(dev);
    net.up(dev);
    net.backend.waitlink(dev);

    # DHCP configuration.
    # net.ipv4.dhcp() will block here until it obtaines an IP address.
    # It doesn't check the obtained address in any way,
    # so as a basic security measure, do not proceed if it is local.
    net.ipv4.dhcp(dev) dhcp;
    ip_in_network(dhcp.addr, "127.0.0.0", "8") test_local;
    ifnot(test_local);

    # Assign IP address to interface, as obtained by DHCP.
    net.ipv4.addr(dev, dhcp.addr, dhcp.prefix);

    # Add default route.
    net.ipv4.route("0.0.0.0", "0", dhcp.gateway, "20", dev);

    # Add DNS servers.
    net.dns(dhcp.dns_servers, "20");

    # Run an external program when network comes up or goes down.
    list("/some/program/to/run/when/up", "argument") do;
    list("/some/program/to/run/when/down", "argument") undo;
    run(do, undo);
}

Note that this will not only handle the network cable being plugged in or out, but also the network interface itself appearing and disappearing (useful if it's USB).

The advantage of using NCD compared to other software like ifplugd is that its design alows for great flexibility. It basically allows you to program your own network config, as opposed to being limited to a limited set of hardcoded capabilities.

UPDATE: I've made Ubuntu packages for BadVPN and NCD. I've updated the wiki with installation and usage instructions for Ubuntu too: http://code.google.com/p/badvpn/wiki/NCD#Running_it

share|improve this answer
    
This really does look useful, do you think you'll get it into the Ubuntu / Fedora repos? –  CoolAJ86 Sep 7 '11 at 19:30
    
Currently I have packages for Arch and Gentoo, and nothing else because I don't use other distros. I may find some time to make packages for other distros. However it's easy to just compile and install it into /usr/local or something, and have make a simple init script to start it. –  Ambroz Bizjak Sep 7 '11 at 19:37
    
Ubuntu packages done, per your request :) –  Ambroz Bizjak Sep 9 '11 at 12:36
add comment

ifplugd handles this situation very well:

ifplugd is a Linux daemon which will automatically configure your ethernet device when a cable is plugged in and automatically unconfigure it if the cable is pulled. This is useful on laptops with onboard network adapters, since it will only configure the interface when a cable is really connected.

ifplugd interfaces with your distribution's native network configuration utilities.

Some features:

  • Uses your distribution's native ifup/ifdown programs.
  • [...]
  • Supports the Linux SIOCETHTOOL (newer, aka ethtool API), SIOCGMIIREG (older, aka mii-diag/mii-tool API) and SIOCDEVPRIVATE (oldest, aka mii-tool API) ioctl()s for getting link status. Release 0.24 introduces support for link detection with the IFF_RUNNING interface flag.
  • [...]
  • Can be configured to ignore short "unplugged" periods (-d option) or short "plugged" periods(-u option)
  • [...]
  • Compatibility mode for network devices which do not support cable detection (-F option)
share|improve this answer
    
My local LUG directed me to netplug, so I went with that instead. Thanks! –  CoolAJ86 Sep 7 '11 at 19:29
add comment

I would suggest using ifplugd over netplugd as it works better and is actively maintained.

ifplugd is a Linux daemon which will automatically configure your ethernet device when a cable is plugged in and automatically unconfigure it if the cable is pulled. This is useful on laptops with onboard network adapters, since it will only configure the interface when a cable is really connected.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.