Quick Code Snippet to Check if the IP is reachable or not


In this post, I am sharing a code snippet that can be used in various different scenarios where you want to check IP reachability using a shell script.

Code Snippet

if ping -c 1 10.1.1.10 &> /dev/null
then
  echo "Cluster node 10.1.1.10 is reachable"
else
  echo "Cluster node 10.1.1.10 is not reachable"
fi


We can integrate it with various other scripts to sent mail alerts or monitoring purposes.


A scenario example I am sharing.

I created a file /shared/CLUSTER_NODE_CHECK/db_nodes which contains all the servers from where I want to check the node ping.

cat db_nodes
server1
server2
server3

The above code snippet, I saved in my shared location(check_cluster_node.sh) and below script will run it on the server name specified in the db_nodes file.


DB_NODES=/shared/CLUSTER_NODE_CHECK/db_nodes
LOGFILE=/tmp/log_files

for prod_server in `cat ${DB_NODES} |grep -v "^#"`
do
echo "" 
echo "Checking ${prod_server}" >> $LOGFILE
ssh ${prod_server} '/shared/CLUSTER_NODE_CHECK/check_cluster_node.sh' >> $LOGFILE 2>&1
if [ $? != 0 ]
then
echo " Error on connecting ${prod_server} " >> $LOGFILE
fi
echo ""
done
if [ `grep "not reachable" ${LOGFILE} |wc -l` -gt 0 ]
then
mail -s "NFS cluster  Reachability Check Failed $(date)" support@funoracleapps.com < $LOGFILE
fi






If you like please follow and comment