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

by on April 10, 2008 · 38 comments

  1. #!/bin/bash
  2. # Simple SHELL script for Linux and UNIX system monitoring with
  3. # ping command
  4. # -------------------------------------------------------------------------
  5. # Copyright (c) 2006 nixCraft project <http://www.cyberciti.biz/fb/>
  6. # This script is licensed under GNU GPL version 2.0 or above
  7. # -------------------------------------------------------------------------
  8. # This script is part of nixCraft shell script collection (NSSC)
  9. # Visit http://bash.cyberciti.biz/ for more information.
  10. # -------------------------------------------------------------------------
  11. # Setup email ID below
  12. # See URL for more info:
  13. # http://www.cyberciti.biz/tips/simple-linux-and-unix-system-monitoring-with-ping-command-and-scripts.html
  14. # -------------------------------------------------------------------------
  15.  
  16. # add ip / hostname separated by white space
  17. HOSTS="cyberciti.biz theos.in router"
  18.  
  19. # no ping request
  20. COUNT=1
  21.  
  22. # email report when
  23. SUBJECT="Ping failed"
  24. EMAILID="me@mydomain.com"
  25. for myHost in $HOSTS
  26. do
  27. count=$(ping -c $COUNT $myHost | grep 'received' | awk -F',' '{ print $2 }' | awk '{ print $1 }')
  28. if [ $count -eq 0 ]; then
  29. # 100% failed
  30. echo "Host : $myHost is down (ping failed) at $(date)" | mail -s "$SUBJECT" $EMAILID
  31. fi
  32. done


4000+ howtos and counting! If you enjoyed this article, join 45000+ others and get free email updates!

Click here to subscribe via email.

  • Stas

    I get error:
    : integer expression expectedcom/ping/ping.sh: line 24: [: 0
    help

  • Mike

    Great lil script.
    Does exactly what I need to monitor my home connection.
    Thankyou very much.

  • Tyler

    Check out Nagios

  • Dude

    Nagios is over the top here

  • aviq indonesia

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

  • remy

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

  • Sergii

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

  • TOROBLANCO2004

    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

  • john

    thanks for posting it. saved me time.

  • stubusy

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

  • HTF

    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?

  • Lewcipher262

    #!/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

    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

  • BirD70

    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.

  • DD

    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

  • nerd

    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)

  • Swati Narang

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

  • Suleman

    Can we print IP with host name in email alert.

Previous Script:

Next Script: