Operating System - HP-UX
1834869 Members
2525 Online
110070 Solutions
New Discussion

Re: Scripting ? for you geniuses

 
Sean OB_1
Honored Contributor

Scripting ? for you geniuses

I want to ping a host and grab some details out of the results.

EX:

ping -c 20 x.x.x.x

Results come back with the following:

20 packets transmitted, 20 received, 0% packet loss, time 9685ms
rtt min/avg/max/mdev = 9.499/17.464/73.811/14.868 ms, pipe 2


I want to set a variable to the % packet loss and another to the max round trip time.

So in this example:
PACKELOSS=0
MAXRTT=73.811

Thoughts on the most efficient way to do this?

TIA and points for all responses.

Sean

8 REPLIES 8
Geoff Wild
Honored Contributor

Re: Scripting ? for you geniuses

Here's the first part:

PACKELOSS=`ping x.x.x.x -n 20 | grep transmitted | awk '{print $ 7}'`


Rgds...Geoff

Proverbs 3:5,6 Trust in the Lord with all your heart and lean not on your own understanding; in all your ways acknowledge him, and he will make all your paths straight.
Steven E. Protter
Exalted Contributor

Re: Scripting ? for you geniuses

ping -c 20 x.x.x.x > /tmp/file.dat

PACKETLOSS=$(cat /tmp/file.dat | awk 'print $6')
tmpmax=$(cat /tmp/file.dat | awk 'print $13')

#You may have to change $6 to $7, just experiment until you get the right data.

You should be able to process $tmpmax through awk to seperate out the slashes and 'print $3' to get the value you want. I don't have exact code for that.

MAXRTT=$(awk -f/ .... '{print $3}'

Hope this is a start.

SEP
Steven E Protter
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
Sean OB_1
Honored Contributor

Re: Scripting ? for you geniuses

Was hoping to do it without writing output to a file AND without running the ping twice.

Sundar_7
Honored Contributor

Re: Scripting ? for you geniuses

Try this sean

ping -c 20 x.x.x.x > /tmp/ping.out
PACKET_LOSS=$(grep "^20 packets" /tmp/ping.out | awk -F, '{print $3}' | awk '{print $1}' | sed 's/\%//'))
MAXRTT=$(grep "^rtt" /tmp/ping.out | awk -F"/" '{print $6}')
Learn What to do ,How to do and more importantly When to do ?
Sanjay_6
Honored Contributor

Re: Scripting ? for you geniuses

Hi Sean,

Try,

ping -c 20 x.x.x.x > /tmp/file.dat
PACKELOSS=`cat /tmp/file.dat |grep "packets transmitted"|cut -f 3 -d ","|cut -f 1 -d "%"`;echo "PACKELOSS=$PACKELOSS"
MAXRTT=`cat /tmp/file.dat |grep "min/avg/max" |cut -f 6 -d "/"`;echo "MAXRTT=$MAXRTT"

Hope this helps.

Regds
Bill Costigan
Honored Contributor

Re: Scripting ? for you geniuses

you could load the ping output into a shell variable and then echo it to the awks mentioned in the other posts

e.g. ping_out=`ping -c 20 x.x.x.x`
PACKETLOSS=`echo $ping_out |awk .... `

Then wouldn't have to run ping twice or write it to a file.
Stuart Whitby
Trusted Contributor

Re: Scripting ? for you geniuses

Don't know why you'd want to avoid putting it to file, but....

RESULTS=$( ping -c 20 x.x.x.x )
PACKETLOSS=$( echo $RESULTS | awk '{ print $7 } | cut -f 1 -d \% )
MAXRTT=$( echo $RESULTS | cut -f 6 -d \/ )

This doesn't seem to be an HP /usr/sbin/ping, btw. I needed to use

RESULTS=$( /usr/sbin/ping nj048sps.unix.us.ups.com -n 10 | tail -2 )

to get similar output to what you have above.
A sysadmin should never cross his fingers in the hope commands will work. Makes for a lot of mistakes while typing.
Hein van den Heuvel
Honored Contributor

Re: Scripting ? for you geniuses


What is the REAL problem you are trying to solve?
What are you goign to do with those variables?

You may find is more attractive/easy/efficient to just do all you have to do in awk or perl, without the need of temp file or temp variables.

Cheers,
Hein.