Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I am trying to perform a simple check on an integer value output from ping. In Linux this was working quite nicely, but the FreeBSD ping outputs a float value with percentage sign suffixed.

LOSS=`/sbin/ping -c 10 -W 1000 -n $IP | grep loss | awk '{print $7}'`

if [ ${LOSS} > ${LIMIT} ]; then
    # Do something here
fi

But since the value of ${LOSS} is a string, the comparison is failing under BSD.

Help, please?

share|improve this question
add comment

2 Answers

up vote 0 down vote accepted

You could alter the awk to split on both spaces and dots in order to pull out the integer part of the loss figure.

ping -c 10 1.2.3.4 | grep loss | awk '{FS = " |[\.]"} {print $7}'
share|improve this answer
add comment

You need to look on the BSDs' ping command. man ping I assume you want de following:

#!/bin/sh

count1=0
count2=0

Ping=$(ping -s 64 10.10.24.6 -c 10 | grep packet | awk '{print $(NF-2)}') # BSD
#Ping=$(ping -s 64 10.10.24.6 -c 10 | grep packet | awk '{print $(NF-4)}')  # Linux

if [ "$Ping" == "0%" ]; then
count1=$((count1 + 1))
echo IP_addr" "UP" "
else
count2=$((count2 + 1))
echo IP_addr" "DOWN" "
fi

echo $count1 IP addresses UP and $count2 IP addresses DOW

In case you don't get the desired result, figure out what parameteres you need to modify in the ping command.

Hope it helps!

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.