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:
# 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

Featured Articles:

Want to read Linux tips and tricks, but don't have time to check our blog everyday? Subscribe to our email newsletter to make sure you don't miss a single tip/tricks.

{ 23 comments… read them below or add one }

1 hardbop200 May 28, 2008 at 9:32 pm

This is great – it really fulfills a need I have. I’m working on expanding it to include the case “unknown host”…

Reply

2 Christian July 26, 2008 at 11:52 pm

I keep getting syntax error: unexpected end of file. Not sure why. The script seems correct to me.

Reply

3 Liam July 28, 2008 at 7:45 am

Christian, if you add a newline at the end of the file it’ll probably be fine; some shells expect this.

Reply

4 moskaskaia July 29, 2008 at 9:49 am

Hi,

I create the file monitor.sh , set permission 755 , and run ./monitor.sh .

the system reply :

./monitor.sh: line 1: imple: command not found

can anyone help me ?

Reply

5 Stefano August 1, 2008 at 8:11 am

I think you’ve missed same characters at the start of the script. Sorry for my terrible english!!
“imple”—> “# Simple”

Reply

6 Niko S. August 24, 2008 at 5:46 pm

use this if you like neat one liners:
[ $(ping -c1 $1 -W1 -w1 | awk '/received/ {print $4}') -eq 1 ] && echo “ONLINE” || echo “OFFLINE”

Reply

7 Dennis August 28, 2008 at 5:59 pm

Hi, great work..

Your script work very well :D

thx :-)

Reply

8 yashwanth February 14, 2009 at 5:55 pm

hi
im new to shell scripts and im unable to understand the above code
can anyone please explain the code above

Reply

9 Peter March 30, 2009 at 12:44 pm

Great script! But I can’t get it to send an e-mail. I’ve just installed mailx, but don’t I need to specify the smtp-server for this to work?? I’ve tried the man-pages and googled around a bit, but I can’t find a solution…

Reply

10 Peter March 30, 2009 at 2:54 pm

After doing much much more googling I’ve finally found how to send mail. For the joys of others: you can do this by installing ssmtp and mailx.

If ssmtp gives you hell installing it (like it did with me), then you should check the hostname of the computer. You can find this in /etc/hosts and you should have a line something like:
“127.0.0.1 yourhost.domain.local yourhost” (and use # to comment the rest out)

After this you should be able to install ssmtp by typing:
dpkg –purge ssmtp
apt-get install ssmtp

configure the ssmtp.conf that is in /etc/ssmtp and should look like this:
root=
mailhub=
rewritedomain=
hostname=

And after all this, you can check if it works by typing:
mail
and ending the mail by typing “.”

Good luck!!

Reply

11 Swati May 26, 2009 at 3:43 am

I am getting an error
line 9: [: -eq: unary operator expected

Can someone help me on this

Reply

12 pituhdo June 20, 2009 at 7:31 pm

@Swati

This normally happens when a host in the HOSTS variable is not known for the machine.

Reply

13 TingWei July 8, 2009 at 1:40 am

Hi Swati,

I believe you have entered hostname instead of ip to test it out right?

If it is unknown hostname , you are probably get this error message. Use IP for the test instead.

Reply

14 The Doctor June 28, 2009 at 4:15 am

I’ll pose a silly question: is there a technique to return a result if the ping attempt fails fr the *local* pc?

I’m attempting to adapt this script to troubleshoot/log a perceived flaky router (somehow still keeping the LAN-side running, abet very slow…), but as Swati & pituhdo have pointed out, it falls face down when the local connection fails.

Reply

15 Vivek Gite June 29, 2009 at 6:39 am

For IP address it will only return 0 (100% fail) or number of packets. Are you using hostname?

Reply

16 Adam July 29, 2009 at 1:15 pm

# add ip / hostname separated by while space

should read

# add ip / hostname separated by white space

my understanding is that you want a space between hostnames
not sure what a while space is
sounds exciting though

Reply

17 mathguy September 12, 2009 at 1:57 pm

Very nice script, you can try with this too.

ping -c3 w3calculator.com
if [[ $? != 0 ]]; then
echo ‘Unavailable’
fi

Reply

18 priya September 24, 2009 at 8:03 pm

how to add multiple host in this script so that it can monitor all the linux machine

Reply

19 ffrr September 24, 2009 at 8:39 pm

what about the line 30th??
“fi” ?? after the ping instructtion, what “fi” is ??

ScriptPing.sh[30]: mail: not found

Reply

20 nix October 14, 2009 at 8:38 am
for i in 192.168.0.{0..25}
do
                ping -i 0.3 -c 1 $i
                if [[ $? -eq 0 ]]
                then
                        echo "$i is online" `date` >> $HOME/guests.txt
                fi
done

Reply

21 Stas November 7, 2009 at 5:13 pm

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

Reply

22 Mike November 12, 2009 at 12:30 am

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

Reply

23 Tyler February 5, 2010 at 7:13 pm

Check out Nagios

Reply

Previous post:

Next post: