Polls

Topics

Testing Connectivity with fping and send mail if any hosts are unreachable

Posted by Vivek on Wednesday November 21, 07 @4:50 pm

fping is one my favorite network profiling / scripting tool. It uses the Internet Control Message Protocol (ICMP) echo request to determine if a target host is responding or not.

Unlike ping , fping is meant to be used in scripts, so its output is designed to be easy to parse.

You can easily write perl / shell script to check a list of hosts and send mail if any are unreachable.

fping command example

Just type the following command to see if we can reach to router:
$ fping router
Output:

router is alive

You can read list of targets (hosts / servers) from a file. The -f option can only be used by the root user. Regular users should pipe in the file via
I/O redirectors (stdin). For example read all host names from ~/.ping.conf file
$ fping < ~/.ping.conf

You can also netmask to ping the entire network i.e generate a target list from a supplied IP netmask. For example, ping the class C 192.168.1.x:
$ fping -g 192.168.1.0/24
or
$ fping -g 192.168.1.0 192.168.1.255

Sample shell script to send email if host is down

#!/bin/bash
HOSTS="router sun printer laptop sony-laptop xbox backup-server"
DLIST=""
for h in $HOSTS
do
  fping -u $h >& /dev/null
  if [ $? -ne 0 ]; then
          echo ${h} host is down send email
          # mail -s "Host ${h} down" admin@you.com </dev/null
  fi
done

Another good example is when you want to perform an action only on hosts that are currently reachable.

#!/usr/bin/perl
$myHosts = ‘cat /etc/hosts.backup | fping -a‘;
foreach $host (split(/\n/,$myHosts)) {
        # take action or call other function
}

Want to stay up to date with the latest Linux tips, news and announcements? Subscribe to our free e-mail newsletter or full RSS feed to get all updates. You can Email this page to a friend.

You may also be interested in...

Discussion on This Article:

  1. Rishi kapur Says:

    It’s good tool but it is not in-built in redhat.
    [root@server1 scripts]# fping
    -bash: fping: command not found
    [

    from where can I download.

  2. vivek Says:

    @Rishi, Download FPING RHEL / CentOS / Fedora Package -
    http://dag.wieers.com/rpm/packages/fping/

  3. Ren Says:

    Show us an example of your ping.conf file contents.

  4. vivek Says:

    cat ping.conf

    router
    nas
    moon
    sun
    oldbox
    dell-laptop
    sony-laptop
    xbox360
    wireless-router
    ps3
    emb1
    emb2
    opendns.com
    myisp.com

Leave a Reply

We encourage your comments, and suggestions. But please stay on topic, be polite, and avoid spam. Thank you very much for stopping by our site!

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Click to hear an audio file of the anti-spam word

Tags: , , , , ~ Last updated on: November 21, 2007

Copyright © 2004-2008 nixCraft. All rights reserved - TOS/Disclaimer - Privacy policy - Sitemap - Powered by Open source software.