[svlug] how to detect ethernet cable is plugged in

svlug@flygirl.com svlug at flygirl.com
Thu May 1 05:53:47 PDT 2003


On Wed, Apr 30, 2003 at 10:54:39PM -0700, HvR wrote:
> my window boxes (yes i  have to have a few for my work) detect when the
> ethernet cable is plugged in or out, how would one detect this under
> linux? any clues?

While still at VA I threw a small program together for this very
reason.  Since we were using DHCP, the laptop would take forever
to boot (trying to get a dhcp address) if there were no cable
plugged in.

(The C code is available at http://www.flygirl.com/sense_link.c)

I added the following to my rc.local:

if /home/lisa/bin/sense_link; then
  /sbin/dhcpcd eth0
else
  /sbin/ifconfig eth0 down
fi

So, if it senses an ethernet connection it tries to get an
address, if not, it downs the interface and saves the time.

Hope this helps...

Lisa




More information about the svlug mailing list