My script doesn't work quite as I want. Can you please show me why?
The aim is to match the numbers returned by ping (text is something like "3 packets transmitted, 3 received, 0% packet loss, time 2003ms")
Ideally, the first two numbers will match, the third number will be 0 and the final number will be about 2000.
So I started to bash together a scribble..
Code:
#!/bin/bash
PING_HOSTS=( list of urls )
PING_COUNT=3
# capture 2 groups of numbers ([0-9]+){2}
regex='([0-9]+)([0-9]+)'
echo " * Started"
for host in ${PING_HOSTS[@]}
do
result=`ping -c $PING_COUNT -q $host | grep "received"`
echo $result $host
if [[ $result =~ $regex ]]
then
echo "capture[1] : ${BASH_REMATCH[1]}"
echo "capture[2] : ${BASH_REMATCH[2]}"
else
echo "unexpected error"
fi
done
The 1st scripting problem:
capture[1] and capture[2] are not the expected values (i.e. 3, 3). What have I done wrong in the regex?

-----------
Also, can you please speculate on the suitability of this type of script for my problem?
My problem: I have some machines and they periodically become unresponsive (i.e. once every 2 weeks or so) and the server logs do not show anything unusual.
An external watchdog is of little to no value because the downed machines cannot be restarted externally.
My intention is to run the above script continuously (when the script is corrected and finished by you guys), and if it detects 2+ ping failures then it will restart the network service or reboot the VM.
Can my approach be bettered, and is Bash the most dependable platform for this task?
Many thanks
