Reputation: 875
I have the below simple ping monitor script
#!/bin/bash
while true; do
HOST=8.8.8.8
HOST2=8.8.4.4
if [[ "$(fping -I -r 1 $HOST | awk '{print $3}' )" = "alive" ]]; then
sleep 1
echo "Internet is UP"
else
if [[ "$(fping -r 1 $HOST2 | awk '{print $3}' )" != "alive" ]]; then
sleep 1
echo "Internet is Down"
fi
fi
done
What i'd like to do is to be able to ping in parallel and incorporate into my above script i.e.
fping -a -A -c 1 8.8.8.8 8.8.4.4 1.1.1.1
And at least one works then "internet is up" but if all fail "internet is down"
Upvotes: 1
Views: 91
Reputation: 3572
Try this:
while true; do
if fping -r1 $HOST $HOST2 | grep -q alive; then
sleep 1
echo "Internet is UP"
else
sleep 1
echo "Internet is Down"
fi
done
Alternatively you can set
hosts=( 8.8.8.8 8.8.4.4 )
... with any number of ip addresses and use ...
if fping -r1 "${hosts[@]}" | grep -q alive; then
Upvotes: 1
Reputation: 35256
Start by storing the ip/host addresses in an array:
hosts=( 8.8.8.8 8.8.4.4 1.1.1.1 91.91.91.91)
Feeding the array to the fping
call:
$ fping -r1 "${hosts[@]}"
8.8.8.8 is alive
8.8.4.4 is alive
1.1.1.1 is alive
91.91.91.91 is unreachable
fping
+ grep
to determine current status:
status=Down
fping -r1 "${hosts[@]}" | grep -q alive && status=UP
echo "Internet is ${status}"
Taking for a test drive:
With hosts=( 8.8.8.8 8.8.4.4 1.1.1.1 91.91.91.91 )
this generates:
Internet is UP
With hosts=( 91.91.91.91 )
this generates:
Internet is Down
If we negate the conditional we can loop while the internet is down (no need for the status
variable in this case):
while ! fping -r1 "${hosts[@]}" | grep -q alive
do
echo "Internet is Down"
sleep 1
done
echo "Internet is UP"
NOTES:
fping
callfping
calls in parallel they also said they wish to run fping -a -A -c 1 8.8.8.8 8.8.4.4 1.1.1.1
(I take this to imply the need to handle a variable number of ip/host addresses) ...fping
handles multiple addresses - does it run them in parallel or serially? - so if OP is looking for even more parallel capability then it may be necessary to bring xargs
or parallel
into the mixUpvotes: 2