|
|
有UNIX 下的shell 高手吗? 帮看一下这个脚本做什么的,写的水平如何?
#!/bin/sh
TMPFILE="lastcheck.tmp"
[ -f $LOG_RUNNINGCONF ] || exit 1
usage() {
echo "Usage: $(basename $0) [IP/HostName/File] [Interval (seconds)] [Total] [Report Email]"
echo "IP -- Dest IP or HostName or File that contain IP list to monitor"
echo "Interval -- Check interval"
echo "Total -- Total check number in a row"
echo "Report's Email -- Email address to send mail to"
exit 2
}
sanity_check()
{
# check Total argument number
[ $# -ne 4 ] && usage
# Check Valid input for interval
expr $2 + 1 1>/dev/null 2>$1
if [ $? -gt 0 ]; then
echo "Please input digital for Interval"
exit 1
else
[ $2 -gt 5 ] && echo "Warning: Interval should be less than 5 seconds" && exit 2
fi
# Check Valid input for Total
expr $3 + 1 1>/dev/null 2>$1
if [ $? -gt 0 ]; then
echo "Please input digital for Total count"
exit 1
else
[ $3 -gt 10 ] && echo "Warning: Total try count should be less than 10 seconds" && exit 3
fi
}
action() {
echo "0" > $TMPFILE
while [ 1 ]
do
sanity_check $@
eval ping -c $3 -i $2 $1 1>/dev/null 2>$1
if [ $? -gt 0 ]; then #Ping Failed
#echo "Ping failed"
currentFlag=1
lastFlag=$(cat $TMPFILE)
if [ $lastFlag -ne $currentFlag ]; then
echo "sendmail failed"
echo "1" > $TMPFILE
fi
else #Ping Ok
echo "Ping Ok"
currentFlag=0
lastFlag=$(cat $TMPFILE)
if [ $lastFlag -ne $currentFlag ]; then
echo "sendmail OK"
echo "0" > $TMPFILE
fi
fi
done
}
#Main
#echo $@
#echo $#
trap "rm lastcheck.tmp && exit" 1 2 15
case "$1" in
-h | --help) usage;;
*) action $@;;
esac
#Exit
exit 0
『声明:以上内容为本站网友《红男爵》原创,转载需征得原作者同意并注明转载自www.hlgnet.com』 |
|
|
|