Operating System - HP-UX
1827073 Members
3578 Online
109713 Solutions
New Discussion

Extracting ping information

 
SOLVED
Go to solution
Sanjay Verma
Super Advisor

Extracting ping information

Hi Friends,

I would like to extract the output of the ping command in a comma deliminated format.

For e.g.,
$ ping microsoft.com -n 2
This will display the last 2 lines as:
8 packets transmitted, 8 packets received, 0% packet loss
round-trip (ms) min/avg/max = 4/6/13

I would like to get the output in the output file as:

-----------------------------------------------
MICROSOFT.COM DATE/TIME MIN,AVG,MAX 4,6,13
----------------------------------------------

I can use the following command to get the last 2 lines:
$ ping microsoft.com -n 2 | grep -e

Hope this's not a difficult steps.

Thankx
Sanjay

Co-operation - The biggest chain reaction
10 REPLIES 10
Andreas Voss
Honored Contributor

Re: Extracting ping information

Hi

try this:

host=microsoft.com
ping $host -n 2 |awk -vhost=$host '/^round-trip/{
gsub("/",",",$NF)
print host " '"$(date '+%m/%d/%Y %H:%M')"' " "MIN,AVG,MAX " $NF
}'

Regards
Ollie R
Respected Contributor

Re: Extracting ping information

Hi Sanjay,

Try something like the following - I'm well aware there are shorter ways to reach the same results but the commands have been written for clarity!!!

SYS=hpx87
STAT=`ping $SYS -n 2 | tail -1`
NUMS=`echo $STAT | awk '{print $5}'`
MIN=`echo $NUMS | cut -d'/' -f1`
AVG=`echo $NUMS | cut -d'/' -f2`
MAX=`echo $NUMS | cut -d'/' -f3`
echo "$SYS : `date` : $MIN,$AVG,$MAX"

Ollie.
To err is human but to not award points is unforgivable
Sanjay Verma
Super Advisor

Re: Extracting ping information

Hi Andreas,

I really appreciate for your quick help. I need to further add the packet loss values after the MIN/AVG/MAX values, so that it can read as MIN/AVG/MAX/PCKTLOSS w,x,y,z.

Plus if it's not able to ping the server at any point of time, it should redirect the error mesg. to another output file.

Thanks once again.
Sanjay
Co-operation - The biggest chain reaction
Robin Wakefield
Honored Contributor
Solution

Re: Extracting ping information

Hi Sanjay,

Try something like:

========================================
#!/usr/bin/ksh

file=/tmp/ping.$$
host=$1

ping $host 8 -n 1 > $file 2>&1
grep -q -e unknown -e 100% $file
if [ $? -eq 0 ] ; then
cat $file >> /tmp/ping.err
rm $file
exit 1
fi
rm $file

date=`date "+%d/%m/%y-%H:%M"`

ping $host -n 2 |
awk '/packet loss/{pl=$(NF-2)}
/round-trip/{n=split($NF,v,"/")}
END{printf("%s %s %s,%s,%s,%s\n",host,date,v[1],v[2],v[3],pl)}' host=$host date="$date"
=============================================

Errors stored in /tmp/ping.err

Rgds, Robin.
Sanjay Verma
Super Advisor

Re: Extracting ping information

Hi Robin,

Thanks for the details. I really appreciate. I've run the script and is working perfectly fine but when the host is not reachable it gives the following error mesg:

cat: Cannot use /...../ping:xxxx as both input and output.

Hope you can light something on this.

Regards,
Sanjay
Co-operation - The biggest chain reaction
Robin Wakefield
Honored Contributor

Re: Extracting ping information

Hi Sanjay,

Can you show me the output of the 1st ping command for this host please?

Rgds, Robin.
Sanjay Verma
Super Advisor

Re: Extracting ping information

Hi Robin,

The output of the command is:

myserver.com 23/08/02-09:39 11,14,18,0%

But when the host is unreachable (wrong host name) it gives the following output:

cat: Cannot use /home/skverma/SCRIPTS/tmp/ping.test as both input and output.

Hope this is what you requried?

~Sanjay
Co-operation - The biggest chain reaction
Sanjay Verma
Super Advisor

Re: Extracting ping information

Hi Ollie,

Thanks for your contribution. Yes, I've checked and it's working fine but I would like to enhance it to include the packet loss also and if the server is not reachable, the error to be re-directed in some other output file.

Hope this's not very difficult.

~Sanjay
Co-operation - The biggest chain reaction
Sanjay Verma
Super Advisor

Re: Extracting ping information

Hi Ollie,

I've modified the script like this:

$ cat 3_script
HOST=myserver.com
STAT=`ping $HOST -n 2 | tail -1`
NUMS=`echo $STAT | awk '{print $5}'`
MIN=`echo $NUMS | cut -d'/' -f1`
AVG=`echo $NUMS | cut -d'/' -f2`
MAX=`echo $NUMS | cut -d'/' -f3`

NO_PING=`ping melsrv1 -n 1 | tail -2 | grep -i loss`
A_OUTPUT=`echo $NO_PING | awk '{print $7}'`
LOSS=`echo $A_OUTPUT | cut -d '%' -f1`

echo "$HOST: `date` : "MIN,AVG,MAX,LOSS: " $MIN,$AVG,$MAX,$LOSS"

It's been a long time since I've brushed my scripting skill. But now only I need to capture the output in other file if the host is not available.

Thanks
Sanjay
Co-operation - The biggest chain reaction
Sanjay Verma
Super Advisor

Re: Extracting ping information

Any further updates on this Guys??

SKV
Co-operation - The biggest chain reaction