_____________________________________________________________________
724-746-5500 | b lackb o x.co m
Page 195
The above command will cause the
ping-detect
script to continuously ping the host at 192.168.22.2
which is the router. If the router crashes, it will no longer respond to ping requests. If this happens, the
two commands
pmpower
and
date
will run. The output from these commands is sent to the file
/tmp/output.log
so that we have a record. The
ping-detect
is also run in the background using the
"&"
.
Remember the
rc.local
script only runs by default when the system boots. You can manually run the
rc.local
script or the
ping-detect
script if desired.
The
ping-detect
script
The above is just one example of using the
ping-detect
script. The idea of the script is to run any number
of commands when a specific host stops responding to ping requests. Here are details of the
ping-detect
script itself:
#!/bin/sh
# Usage: ping-detect HOST [COMMANDS...]
# This script takes 2 types of arguments: hostname/IPaddress to ping, and the commands to
# run if the ping fails 5 times in a row. This script can only take one host/IPaddress per
# instance. Multiple independent commands can be sent to the script. The commands will be
# run one after the other.
#
# PINGREP is the entire reply from the ping command
# LOSS is the percentage loss from the ping command
# $1 must be the hostname/IPaddress of device to ping
# $2... must be the commands to run when the pings fail.
COUNTER=0
TARGET="$1"
shift
# loop indefinitely:
while true
do
# ping the device 10 times
PINGREP=`ping -c 10 -i 1 "$TARGET" `
#get the packet loss percentage
LOSS=`echo "$PINGREP" | grep "%" | sed -e 's/.* \([0-9]*\)% .*/\1/'`
if [ "$LOSS" -eq "100" ]
then
COUNTER=`expr $C 1`
else
COUNTER=0
sleep 30s
fi
if [ "$COUNTER" -eq 5 ]
then
COUNTER=0
"$@"
sleep 2s
fi
done