I asked before for help with a ping script that should ping some hosts and email me if it any ping fails. This works fine.
Now I want to modify that script (see below) so it emails me if it succeeded only. So basically I want the script to ping few hosts I know they're not up and if any of those hosts returned ping, then I want the script to email me. Here's the script I currently have:
#!/bin/bash
ping_targets="server1 server2 server3 server4 server5"
failed_hosts=""
for i in $ping_targets
do
ping -c 1 $i > /dev/null
if [ $? -ne 0 ]; then
if [ "$failed_hosts" == "" ]; then
failed_hosts="$i"
else
failed_hosts="$failed_hosts, $i"
fi
fi
done
if [ "$failed_hosts" != "" ]; then
echo $failed_hosts| mailx -s "Failed ping targets" email@domain
fi