Reputation: 1
I have a handy dandy shell script that cycles through a list of IP addresses and echoes out the percentage of packet loss (below).
I'd love to weave in an if/then statement to only echo out results if the percent does NOT equal 0.0%.
Your suggestions most appreciated!
#!/bin/bash
HOSTS="192.168.99.24 192.168.99.23"
COUNT=10
SIZE=1400
for myHost in $HOSTS
do
ping -q -n -s $SIZE -c $COUNT $myHost | awk -v host=$myHost '/packet loss/ {print host, $7}'
done
Upvotes: 0
Views: 215
Reputation: 17104
If you just want to check a list of hosts if each host is alive or not, then I'd suggest to ditch using ping
at all for this purpose and use fping
, which is much better scriptable than regular ping
and solves this problem in one-liner:
$ fping -q -c $COUNT -b $SIZE $HOSTS | grep ': xmt' | grep -v '%loss = .*/0%'
192.168.1.2 : xmt/rcv/%loss = 12/0/100%
192.168.1.3 : xmt/rcv/%loss = 12/0/100%
192.168.1.4 : xmt/rcv/%loss = 12/0/100%
Invocations of grep
here are used to (1) grep only for resulting lines, (2) remove unwanted lines with 0% loss which look like this:
192.168.1.1 : xmt/rcv/%loss = 12/12/0%, min/avg/max = 1.08/1.11/1.19
fping
scales really well up to millions of hosts and can be used to ping a list of hosts in a file:
fping -q -c $COUNT -b $SIZE -f <host-list.txt | grep ': xmt' | grep -v '%loss = .*/0%'
Upvotes: 0
Reputation: 530872
I'm assuming $7
is the percentage (it's $6
for me). Just put an extra condition in your awk
script.
awk -v host=$myHost '/packet loss/ && $7!="0%" {print host, $7}'
Upvotes: 1