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

by on April 10, 2008 · 37 comments

#!/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:
# http://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
4000+ howtos and counting! If you enjoyed this article, join 45000+ others and get free email updates!

{ 37 comments… read them below or add one }

21 Stas November 7, 2009

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

22 Mike November 12, 2009

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

23 Tyler February 5, 2010

Check out Nagios

24 Dude March 30, 2010

Nagios is over the top here

25 aviq indonesia September 2, 2010

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

26 remy September 25, 2010

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

27 Sergii November 23, 2010

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

28 TOROBLANCO2004 December 10, 2010

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

29 john January 13, 2011

thanks for posting it. saved me time.

30 stubusy January 20, 2011

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

31 HTF April 3, 2011

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?

32 Lewcipher262 November 2, 2011

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

33 Aryab December 20, 2011

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

34 BirD70 March 20, 2012

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.

35 DD March 21, 2012

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

36 nerd March 24, 2012

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)

37 Swati Narang April 2, 2012

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

Leave a Comment

You can use these HTML tags and attributes for UNIX commands or shell scripts: <strong> <em> <ol> <li> <u> <ul> <blockquote> <pre> <a href="" title="">
What is 8 + 13 ?
Please leave these two fields as-is:
Are you a human being? Solve the simple math so we know that you are a human and not a script.



Tagged as: eq, for loop, grep command, hostname, linux, loop method, mail command, monitor host using ping script, ping command, ping request, shell loops, shell script, space hosts, unix, unix system

Previous Script:

Next Script: