Infrequent Geek

Know How To Computer Blog

Linux Scripting -Ping Server

with 4 comments

Thanks to this site for supplying the original source.  I have another script I’ll be walking through.  If you go to the previous mentioned site you can see how to get e-mail notifications that the server is down.

In this tutorial we’ll use the ping, grep, and awk commands.  I’ll briefly cover each command, to get the full information on each command I suggest google.

Start out by assigning the server you want to ping to a variable.

HOST=”dailykermit.wordpress.com”

You should probably specify the number of times you wish to ping the server each time the command runs.

PINGCOUNT=3

Now the next step is taking our variables and combining it with the ping command

count=$(ping -c $PINGCOUNT $HOST)  or count=$(ping $HOST –c $PINGCOUNT)

Make sure you set it equal to a variable, so we can store the value that is returned to the command line.

You must include the –c, it specifies that you’ll include the number of times to ping a server.  It’s the same as doing ping dailykermit.wordpress.com –n 3 from the CMD prompt in Windows.  If you’ve entered a valid host you should see something similar to this:

samir@foo:~> ping dailykermit.wordpress.com -c 3

PING lb.wordpress.com (72.233.2.59) 56(84) bytes of data.

64 bytes from lb.wordpress.com (72.233.2.59): icmp_seq=1 ttl=64 time=0.114 ms

64 bytes from lb.wordpress.com (72.233.2.59): icmp_seq=2 ttl=64 time=0.074 ms

64 bytes from lb.wordpress.com (72.233.2.59): icmp_seq=3 ttl=64 time=0.084 ms

— lb.wordpress.com ping statistics —

3 packets transmitted, 3 received, 0% packet loss, time 2002ms

rtt min/avg/max/mdev = 0.074/0.090/0.114/0.020 ms

Most of this information isn’t really needed.  The most important part is the ping statistics.  To only receive packet information we’ll use global / regular expression / print, you may know this as grep (duh!)

To filer the information we need we’ll use some pipes (No. Not that kind of pipe.)

count=$(ping  -c $PINGCOUNT $HOST | grep received)

Combining ping with grep will yield the following output:

3 packets transmitted, 3 received, 0% packet loss, time 2000ms

Ok. We’re making progress. We’ve got it down to one line.  However, that’s still too much information.  This is where awk comes in.  I’ll be honest I don’t know what awk stands for.  I tried doing some research on but I had no luck.  Though, I do know that it’s used for manipulating strings and tables of data.

By adding awk our little command should look like this:

count=$(ping -c $PINGCOUNT $HOST | grep ‘received’ | awk -F ‘,’ ‘{ print $2 }’)

Don’t be scared if it looks crazy. It’s really simple.  The –F flag indicates that we want to omit the field separator ‘,’ (the comma.)   ‘{ print $2 }’ indicates that we want to print whatever is located in the second column.  Make sure you have a space between the curly braces and p, and at the end between the $2 and braces, else it’ll think it’s a different command.  Bash scripting is picky about spacing, keep that in mind.  Now you should get and output similar to this:

3 received

I am sorry to say, but that’s still more information than we need.  What we need is that three, but right now it’s being stalked by received.  So the next step is to break-up three and received (don’t worry, this is the good kind of break-up.)  Now we use awk again (you are going to learn to love awk. I know I have.)

count=$(ping -c $PINGCOUNT $HOST | grep ‘received’ | awk -F ‘,’ ‘{ print $2 }’ | awk ‘{ print $1 }’)

That my friend breaks up the three and received, and yields just the three –all alone.

Now that we have separated our information we can move on to our logic.

The first thing you want to do is check the value of count if it isn’t zero then we don’t have to worry about doing anything.  However, if it’s zero we’ll have to inform someone that the node is down or the network is just really, really, really slow.

To test count we do the following:

if [ $count –eq 0 ]; then

#server received no packets

echo “OH NO!”

fi

You can replace the echo with anything you want.  For example, the script that I wrote starts up a license process on the back-up server.

That’s it, you’re final script should look like this:

#!/bin/bash

HOST=”dailykermit.wordpress.com”

PINGCOUNT=3

count=$(ping -c $PINGCOUNT $HOST | grep ‘received’ | awk -F’,’ ‘{ print $2 }’ | awk ‘{ print $1 }’)

if [ $count –eq 0 ]; then

#server received no packets

echo “OH NO!”

fi

All that’s left to do is save it in whatever editor you were working in, I prefer vi, and you’re set.

Enjoy.

Advertisements

Written by Samir Banjanovic

June 25, 2009 at 1:30 pm

Posted in Informational, Tutorial

4 Responses

Subscribe to comments with RSS.

  1. Thanks, seen this before but your explanations helped out a lot. I did have a question though. Do we really need the 2 awk command? I just want to better understand. I get the same result using just one. But was not sure if it was there for a reason.
    ping -c 3 192.168.22.100 | grep received | awk ‘{print $1}’

    Still yeilds a 3 for me at the prompt.

    John

    July 7, 2009 at 1:31 am

    • You want to be careful with what you did. You’re outputting the wrong value. Since you specified the first column it’s showing you the number of packets transmitted.

      3 packets transmitted, 3 received, 0% packet loss, time 2000ms
      ———–column 1————column 2——–column 3——–column 4

      In your script the 3 corresponds to the number of packets transmitted. If you replace the “awk ‘{ print $1 }’ ” with awk ‘{ print $2 }’ you should get the output “3 received”

      Since you took out the awk that specifies to take away the commas you’re breaking the actual output. In the script shown in the tutorial you took out the commas, then you stepped to the 2nd column (the received column), once in that column you went to the first sub column, which is the 3.

      What you basically did is ignore sub columns and take the values from the parent columns. Be careful with the values you use. As always you want to test your script by taking your designated host offline and seeing if you get any values. Since it’s offline you should be getting a 0, with your script you’ll still get a 3.

      Samir Banjanovic

      July 7, 2009 at 1:58 am

    • I just realized what else could be happening. It could be that in the shell version you’re using it’s skipping straight to the column for received packets, since you’re using grep. To be honest I would play with it and see what results you get. But I wouldn’t be surprised if it’s because of the first reason I listed. I would check it to make sure. I would also replace your grep, but keep everything else the same and see what results you yield. Play with the script and see what results you get. But make sure to take the host offline and see if it’s returning zero or three

      Samir Banjanovic

      July 7, 2009 at 2:08 am

  2. Thanks for the response. After playing around with it a bit more, I found that I was getting the wrong answer. It was clear when I attempted to ping an address that was not assigned. Which was confirmed after reading your response. Thanks again.

    John

    July 7, 2009 at 2:14 am


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

%d bloggers like this: