[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]

Re: nic + detecting the presence of a carrier



On 9/1/06, Gregory Machin <gregory machin gmail com> wrote:
Stunning data ... but doesnt tell me if the nic is unpluged .. :-(


The suggestion by Frank works like a charm.  Either of the two works
very well to detect if the NIC is working or not.  Then something as
simple as:

/sbin/mii-tool eth0 | grep "link ok" && <insert command you wish to
execute if link ok>
with the command you wish to execute following the &&

That will only execute if the grep is successful (so the link is ok).
If not, the output of the mii-tool is "no link" therefore the grep
will return a negative result which in turn will not execute the
command after the &&.

If you prever to use ethtool, then your command would be:

/sbin/ethtool eth0 | grep "Link detected: yes" && <insert command you
wish to execute if link ok>

If you are looking at restarting a number of services, I would
recommend creating a function within your script and then simply
calling the function if the link is ok.

Here is a suggested script you can start with (I've tested it and it
worked properly, restarting services when nic detected, echoing the
error and the output of the ethtool when NIC was disconnected).  Note
that I use && as well as ||.  If successful grep, command after && is
executed.  If not successful then the command after || is executed.
This may not meet your needs, but I'm throwing it out there just in
case.

Jacques B.

#!/bin/bash
#script that will execute a function if the link is detected
#
#########################################
#          FUNCTIONS
#########################################
#function restart_services
restart_services ()
{
/etc/init.d/httpd restart && echo "Successfully restarted httpd" ||
echo "httpd failed to restart"
/etc/init.d/sshd restart && echo "Successfully restarted sshd" || echo
"sshd failed to restart"
}

#function trouble_shoot
trouble_shoot ()
{
echo "Link not detected.  See output below..."
/sbin/ethtool eth0
}

# Start of script...

echo "Checking NIC and taking appropriate action..."

/sbin/ethtool eth0 | grep "Link detected: yes" && restart_services ||
trouble_shoot

echo "Done!"


Jacques B.


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]