#!/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 via:
- RSS feed or Weekly email newsletter
- 39 comments... add one ↓
Category | List of Unix and Linux commands |
---|---|
File Management | cat |
Firewall | Alpine Awall • 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 | Alpine • CentOS 8 • Debian 10 • Firewall • Ubuntu 20.04 |
In my personal version I modified the program with this:
48374632603d1b1919bd5_000001
Can we print IP with host name in email alert.
Awesome post.
Please also share that how to restart a server using unix.
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)
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
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.
#!/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
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
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?
thanks for posting it. saved me time.
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
Really cool script! It saved time =)
Thanks to author
if [ ${count:-0} -eq 0 ] || [ -z “$count” ]; then
I got “-eq: unary operator expected” with the original script…
you should use like this if[[ ${count:-0} -eq 0 ] || [ -z “$count” ]]
if [ $count -eq 0 ] || [ -z “$count” ]; then
Check out Nagios
Nagios is over the top here
Great lil script.
Does exactly what I need to monitor my home connection.
Thankyou very much.
I get error:
: integer expression expectedcom/ping/ping.sh: line 24: [: 0
help
what about the line 30th??
“fi” ?? after the ping instructtion, what “fi” is ??
ScriptPing.sh[30]: mail: not found
how to add multiple host in this script so that it can monitor all the linux machine
Very nice script, you can try with this too.
ping -c3 w3calculator.com
if [[ $? != 0 ]]; then
echo ‘Unavailable’
fi
# 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
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.
For IP address it will only return 0 (100% fail) or number of packets. Are you using hostname?
I am getting an error
line 9: [: -eq: unary operator expected
Can someone help me on this
@Swati
This normally happens when a host in the HOSTS variable is not known for the machine.
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.
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!!
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…
hi
im new to shell scripts and im unable to understand the above code
can anyone please explain the code above
Hi, great work..
Your script work very well :D
thx :-)
use this if you like neat one liners:
[ $(ping -c1 $1 -W1 -w1 | awk ‘/received/ {print $4}’) -eq 1 ] && echo “ONLINE” || echo “OFFLINE”
I think you’ve missed same characters at the start of the script. Sorry for my terrible english!!
“imple”—> “# Simple”
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 ?
Christian, if you add a newline at the end of the file it’ll probably be fine; some shells expect this.
I keep getting syntax error: unexpected end of file. Not sure why. The script seems correct to me.
This is great – it really fulfills a need I have. I’m working on expanding it to include the case “unknown host”…