1834009 Members
4447 Online
110063 Solutions
New Discussion

Re: script help

 
SOLVED
Go to solution
Nick D'Angelo
Super Advisor

script help

All,

I have a ping script that I want to build in a condition that it emails me if during the ping routine there are any packet losses.

Here is my script that pipes the output to a log file.

In the log file, I am not sure how to grep for anything other than 0% in the packet loss.

Thank you in advance,

!/usr/bin/ksh
#
LOG_DIR=`date +"/tmp/%d"`;export LOG_DIR
mkdir -p $LOG_DIR >/dev/null 2>&1
RETVAL=0
PATH=$PATH:/usr/sam/lbin:/usr/sam/bin:/usr/bin:/usr/sbin:/sbin;export PATH
#
echo "Subject: ping sapdev Report for "`date` >>$LOG_DIR/pingsap.$$
date >> $LOG_DIR/pingsap
ping 10.10.12.52 -n 20 >>$LOG_DIR/pingsap
#
# mail out results
#
#sendmail nickd < /tmp/pingsap.$$
rm $LOG_DIR/pingsap.$$
Always learning
15 REPLIES 15
someone_4
Honored Contributor

Re: script help

Hi Nick,
Here is the script that I use. But I dont look for packet loss. This catches connection loss. You can modify it to email you if a host is down or fails. iphosts is a file with all the hostnames of ip addresses that you want to check in one collum. Like so:

servera
serverb
serverc

#!/bin/sh
LANG=C
HOSTNAME_FILE=iphosts
for host in $(cat $HOSTNAME_FILE)
do
ping $host -n 1 | grep -q '1 packets received'
if [ $? = 0 ]
then
echo "$host: OK"
else
echo "$host: FAIL"
fi
done
Holger Knoppik
Valued Contributor

Re: script help

Hi Nick,

try with
grep 'loss' | awk '{print $7}'
HTH

RGDS, Holger
Live long and prosper!
H.Merijn Brand (procura
Honored Contributor

Re: script help

Generic grep on positive numbers using perl

l1:/u/usr/merijn 102 > for 10 12 100 13.8 0 9 0.0 120
foreach? echo "--- $i%"
foreach? echo "$i% packet loss" | perl -ne 'm/([\d.]+)\s*%/&&$1>0&&print'
foreach? end
--- 10%
10% packet loss
--- 12%
12% packet loss
--- 100%
100% packet loss
--- 13.8%
13.8% packet loss
--- 0%
--- 9%
9% packet loss
--- 0.0%
--- 120%
120% packet loss
l1:/u/usr/merijn 103 >
Enjoy, Have FUN! H.Merijn
S.K. Chan
Honored Contributor

Re: script help

....
#
# mail out results
#
PCT=`grep \^20 $LOG_DIR/pingsap | awk -F", " '{print $3}' | awk -F"%" '{print $1}'`
if [ $PCT = 0 ]
then
:
else
sendmail nickd < /tmp/pingsap
fi
done
rm $LOG_DIR/pingsap
S.K. Chan
Honored Contributor

Re: script help

take note of the space after the "," in the first awk statement ..
Sachin Patel
Honored Contributor
Solution

Re: script help

Hi Nick,
Here is what I uses

#!/bin/csh
set PING = "/etc/ping"
set COUNT = 3

set RESULT = `$PING $PLOT_SERVER -n $COUNT |grep "packet loss" | awk '{print $7}
' | sed 's/%//'`
if ($RESULT >= 100) then
echo "$PLOT_SERVER is down"
/usr/bin/mailx -s "$PLOT_SERVER is down `date`" sachin
exit
endif

Sachin
Is photography a hobby or another way to spend $
Nick D'Angelo
Super Advisor

Re: script help

S.K.

Your script sample almost works, however, I only want to grep for % >0 on each ping which is setup to run through cron.

Your script is mailing out the top of the log file on all instances and should only be sending mail on % > 0.

Nickd
Always learning
Nick D'Angelo
Super Advisor

Re: script help

Sachin,

Thanks for your script, but it give me an error, Unmatched `.

??

Always learning
Tom Danzig
Honored Contributor

Re: script help

Here's one I wrote a while ago (backup when I liked csh). It was mainly to check response time from our default gateway router.
Tom Danzig
Honored Contributor

Re: script help

Forgot to mention that the above script is written for Sun. Modify accordingly if you want to use it on HP for any reason.

Tom
S.K. Chan
Honored Contributor

Re: script help

First of all , sorry for the "done" line, it should not be there .. :)
Try this ..

#
# mail out results
#
PCT=`grep \^20 $LOG_DIR/pingsap | awk -F", " '{print $3}' | awk -F"%" '{print $1}'`
if (($PCT > 0))
then
sendmail nickd < /tmp/pingsap
else
:
fi
rm $LOG_DIR/pingsap
Chris Richings
New Member

Re: script help

Hi

Here is another snippet of using 'ping' that may be useful..

cheers

Chris


ping ${SG_SERVICE_NAME} -n 3 | grep 100% >/dev/null
if [ "$?" = 0 ]; then
echo "Unable to ping${SG_SERVICE_NAME}"
echo "SG package probably down "
exit 1
else
echo "Ping of SG successful"
fi
Ruediger Noack
Valued Contributor

Re: script help

And my suggestion to catch your recently loss value in your logfile:

LOSS=`tail -5 | fgrep loss | awk '{print $7}' | tr '%' ' '`
if [ "$LOSS" -gt 0 ]
then
# your email
fi

Ruediger


Virginia Borraz
New Member

Re: script help

You can test this. It works !


CONN=`ping XX.XX.XX.XX -n 20 |grep "packet loss" | cut -d"," -f3 | cut -d"%" -f1`
if [ $CONN != 0 ]
then
mailx ...... Connection problem
else
Succesful connection
fi
El angel exterminador
Nick D'Angelo
Super Advisor

Re: script help

Virginia,

Your script is almost perfect for me.

I just wanted to include the last three lines of my ping for example:

--spock PING Statistics----
20 packets transmitted, 8 packets received, 60% packet loss
round-trip (ms) min/avg/max = 20/20/21
Thu Apr 25 06:57:46 EDT 2002

To the email so that these Microsoft Network "experts" NOT ;-)have the necessary information.

Thanks.
Always learning