UNIX / Linux Shell Script For Monitoring System network with ping command

#!/bin/bash
# Simple SHELL script for Linux and UNIX system monitoring with 
# ping command
# -------------------------------------------------------------------------
# Copyright (c) 2006 nixCraft project <http://www.cyberciti.biz/fb/>
# This script is licensed under GNU GPL version 2.0 or above
# -------------------------------------------------------------------------
# This script is part of nixCraft shell script collection (NSSC)
# Visit http://bash.cyberciti.biz/ for more information.
# -------------------------------------------------------------------------
# Setup email ID below
# See URL for more info:
# https://www.cyberciti.biz/tips/simple-linux-and-unix-system-monitoring-with-ping-command-and-scripts.html
# -------------------------------------------------------------------------
 
# add ip / hostname separated by white space 
HOSTS="cyberciti.biz theos.in router"
 
# no ping request
COUNT=1
 
# email report when 
SUBJECT="Ping failed"
EMAILID="me@mydomain.com"
for myHost in $HOSTS
do
  count=$(ping -c $COUNT $myHost | grep 'received' | awk -F',' '{ print $2 }' | awk '{ print $1 }')
  if [ $count -eq 0 ]; then
    # 100% failed 
    echo "Host : $myHost is down (ping failed) at $(date)" | mail -s "$SUBJECT" $EMAILID
  fi
done
Get the latest tutorials on SysAdmin, Linux/Unix, Open Source, and DevOps topics:
CategoryList of Unix and Linux commands
File Management cat
Firewall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network Utilities dig host ip nmap
OpenVPN CentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Manager apk apt
Processes Management bg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searching grep whereis which
User Information groups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPN CentOS 8 Debian 10 Firewall Ubuntu 20.04
39 comments… add one
  • José Luis Oct 23, 2016 @ 2:00

    In my personal version I modified the program with this:

    13961224105f6af38575c80_000001

  • Suleman Apr 3, 2013 @ 5:22

    Can we print IP with host name in email alert.

  • Swati Narang Apr 2, 2012 @ 14:01

    Awesome post.
    Please also share that how to restart a server using unix.

  • nerd Mar 24, 2012 @ 20:30

    N00b talking: How do i get my box with crunchbang on it get to email the notification? The script runs tĥrough, but it doesnt email, because my smtp requires authentification. (im not able to make the script pass this auth.)

    Thanx a lot for help, using this for monitoring a big Wireless system with lots of ips.(nonprofit)

  • DD Mar 21, 2012 @ 10:15

    Hi,

    In this script while executing we get multiple mails for multiple servers mentioned how to get all server details in one single mail which loop should i remove in this

  • BirD70 Mar 20, 2012 @ 7:16

    I welcome! Prompt me as to adjust this script on constant repetition if ping is ok? If no ping send 1 msg and exit? cron not to offer.

  • Lewcipher262 Nov 2, 2011 @ 19:12

    #!/bin/bash

    # add ip / hostname separated by white space
    HOSTS=”YOU CAN ADD MULTIPLE IP’S HERE”
    # no ping request
    COUNT=1

    for myHost in $HOSTS;
    do
    count=`ping -c $COUNT $myHost | grep ‘received’ | awk -F’,’ ‘{ print $2 }’ | awk ‘{ print $1 }’`
    if [ $count -eq 0 ]
    then
    echo $myHost “WHAT EVER YOU WANT OUTPUT TO SAY”
    else
    echo $myHost “WHAT EVER YOU WANT OUTPUT TO SAY”
    fi
    done

    The “then” statement between quotes is what will be displayed if online

    The “else” statement between quotes is what will be displayed if not online

    • Aryab Dec 20, 2011 @ 10:50

      Hi Lewcipher,

      From the below script if any ping goes down it immediately throws a mail. But how to configure this script if ping is (okay) it should shoot a mail that all are okay.

      Can u please help me.

      # add ip / hostname separated by white space
      HOSTS=”cyberciti.biz theos.in router”

      # no ping request
      COUNT=1

      # email report when
      SUBJECT=”Ping failed”
      EMAILID=”me@mydomain.com”
      for myHost in $HOSTS
      do
      count=$(ping -c $COUNT $myHost | grep ‘received’ | awk -F’,’ ‘{ print $2 }’ | awk ‘{ print $1 }’)
      if [ $count -eq 0 ]; then
      # 100% failed
      echo “Host : $myHost is down (ping failed) at $(date)” | mail -s “$SUBJECT” $EMAILID
      fi
      done

      Regards,
      Aryan

  • HTF Apr 3, 2011 @ 2:22

    Quick question:
    If I set the ping count to 4 and only one of four will failed will I get the true positive alarm anyway?

  • john Jan 13, 2011 @ 19:33

    thanks for posting it. saved me time.

  • TOROBLANCO2004 Dec 10, 2010 @ 18:31

    How can I put this script on my dd-wrt router so it sends me alerts when one of the pc goes down? I don’t how to make run from my router. any suggestions.

    Thanks

  • Sergii Nov 23, 2010 @ 13:55

    Really cool script! It saved time =)
    Thanks to author

  • remy Sep 25, 2010 @ 10:29

    if [ ${count:-0} -eq 0 ] || [ -z “$count” ]; then
    I got “-eq: unary operator expected” with the original script…

    • stubusy Jan 20, 2011 @ 3:03

      you should use like this if[[ ${count:-0} -eq 0 ] || [ -z “$count” ]]

  • aviq indonesia Sep 2, 2010 @ 8:45

    if [ $count -eq 0 ] || [ -z “$count” ]; then

Leave a Reply

Your email address will not be published. Required fields are marked *

Use HTML <pre>...</pre>, <code>...</code> and <kbd>...</kbd> for code samples.