Ow, thanks. ifplugd is exactly what I needed

(ethtool's link detection only works if the interface is up afaik, thus not rally viable

)
Edit 1:
Sadly though, it doesn't work. even without a cable inserted, /etc/init.d/ifplugd restart leads to:
Code: Select all
ifplugd restart
[ ok ] Network Interface Plugging Daemon...stop eth0...done.
[ ok ] Network Interface Plugging Daemon...start eth0...done.
My ifplugd config contains the following:
Code: Select all
INTERFACES="eth0"
HOTPLUG_INTERFACES=""
ARGS="-l -q -f -u0 -d10 -w -I"
SUSPEND_ACTION="stop"
Edit 2:
Also, when I bring eth0 down manually (without a cable inserted), ifplugd immediately brings it up again. So basically it does exactly the opposite of what it is supposed to do.
Edit 3:
I am actually going to use ethtool or maybe exploit its functionality, as I discovered a loophole in the whole matter. Whilst the "link detected" output is not reliable for my cause, the "Speed:" output actually is. I will provide my script when it's done so that future users don't have to figure it out themselves.
-------------------------------------
Final Edit: This is the final script I wrote, which controls eth0 when executed. If eth0 is up without an existing cable connection, the script will bring down the interface. If eth0 is not up with an existing cable connection, it will bring it up. Have fun =)
Code: Select all
#!/bin/bash
#Extract speed info from ethtool. If speed is 10 Mb/s, no cable is connected. If it is 100 Mb/s, there is a cable present.
SPEED=`ethtool eth0 | grep -i "Speed" | awk '{print $2}' | grep -o '[0-9]*'`
if ifconfig | grep -i "eth0" > /dev/null 2>&1; then
echo "Ethernet interface is already up. Checking Cable connection..."
if [ $SPEED == 100 ]; then
echo "Ethernet connection OK."
else
echo "Error: No cable connected. Bringing down eth0"
ifdown --force eth0
exit
fi
exit
else
if [ $SPEED == 100 ]; then
echo "Cable connection detected. Bringing up eth0..."
ifup eth0
else
echo "Error: No cable connected."
exit
fi
fi
exit