Friday 20 September 2019

dhcp - How to configure `eth0` to retry `dhclient` when unplugged and replugged?


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.



Answer




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

No comments:

Post a Comment

How can I VLOOKUP in multiple Excel documents?

I am trying to VLOOKUP reference data with around 400 seperate Excel files. Is it possible to do this in a quick way rather than doing it m...