Simple BASH Server Check Script

Recently I have been working on some BASH scripts to remotely check the status of various servers.  There are lots of paid and free tools out there to do this kind of monitoring but for those wanting to try out their own scripts below is some useful commands and hints you can use.

Gathering information

There are a number of methods you can use to check if a server is pingable.

Ping:

ping -c 1 10.0.0.1 < /dev/null
if [ $? -ne 0 ]; then
  SUCCESS - DO SOMETHING
else
  FAILED - DO SOMETHING
fi

NMAP:

if ! nmap -sP 10.0.0.1 | grep -q "Host seems down"; then
  FAILED - DO SOMETHING
else
  SUCCESS - DO SOMETHING
fi

Netcat is one of the main tools I use to test connections to servers.  With a simple one line command you can perform ssh, http and other connection tests.

Lets take a look at two examples:

SSH Connection test:

if ! nc -z -w 5 10.0.0.1 22; then
  SSH FAILED - DO SOMETHING
else
  SSH SUCCESSFUL - DO SOMETHING
fi

HTTP Connection test:

if ! nc -z -w 5 10.0.0.1 80; then
  HTTP CONNECTION FAILED - DO SOMETHING
else
  HTTP CONNECTION SUCCESSFUL - DO SOMETHING
fi

The above commands should get you started, put these into a while or for loop and you have a simple script that checks connections to a number of hosts.

cat /path/to/iplist.txt | while read output
do
  PERFORM CHECKS
done
for i in 10.0.0.{1..255}
do
  PERFORM CHECKS
done

Speeding things Up

Now you may find the above a little slow, especially if not every host on the network is live and you have to wait for ping to timeout.

If you move your connection tests into a function and then call that function from your loop you can actually use the ampersand (&) to run the function in the background.

function checkhostsstatus() {
  PERFORM CHECKS
}

while read line
do
  checkhostsstatus $line &
done < iplist.txt
wait

This will run the checks against all your hosts at the same time.  Now you need to be a little careful here as you might be spawning a large number of threads and generating a lot of IO.  I will cover some techniques for handling this in another post.

Final Script

Below is a finished example of the script.  This one just echos out the results to the screen but you can modify it to record the information to a database or to send an email.

#!/bin/bash
function checkhoststatus() {
   ip=$1;
   if ! nmap -sP $ip | grep -q "Host seems down"; then
     echo "Host $ip is down"
   else
     echo "Host $ip is up"
   fi

   if ! nc -z -w 5 $ip 22; then
     echo "Host $ip SSH is down"
   else
     echo "Host $ip SSH is up"
   fi
}
while read line
do
  checkhoststatus $line &amp;amp;
done < iplist.txt

wait

One Reply to “Simple BASH Server Check Script”

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s