Shell Script to Ping Hostnames and Send SMS if Host is not Available

We use the below procedure to ping hosts and based on availability send SMS to our team. The SMS is send by calling a web service from the BASH Shell script using curl 
The hosts are defined in a file called hosts.txt

-- Shell Script --
#!/bin/bash
current_time=$(date "+%Y%m%d%H%M%S%3N")
#### Above Date Format is in MilliSeconds Accuracy ###
cd /home/online
rm -rf /home/online/SMSHOST.TXT
date
#### Below Code Will Loop the Hosts.TXT file for the Ping Probes ####
cat /home/online/hosts.txt | while read output
do
ping -w 30 -c 1 $output > /dev/null
if [ $? -ne 0 ]
then
SMSTEXT='The+Host+'$output'+is+not+reachable.+Please+Contact+the+Database+Team.'
echo "Host $output is Down"
curl "http://192.168.10.100:8080/SMSConnect/SendServlet?application=http_gw2&password=http_gw2&content=$SMSTEXT&destination=9998887190&source=97249&mask=COMPANY"
else
echo "Host $output is Up"
fi
done

— hosts.txt file containing list of hostnames —
proddb01
proddb02
proddb03
drdb01
drdb02
drdb03

Category: BashLinuxScript

Tags:

Leave a Reply

Article by: Shadab Mohammad