Your Linux Data Center Experts

Recently a friend was saying that one of his co-workers doesn't really write scripts, and had asked my friend to write a script to ping a bunch of hosts. The use was for inventory in preparation for a big move, to make sure they weren't missing hosts or IPs that needed to be moved.

I immediately recommended “fping”. It's default mode will read target names from stdin and ping them, and it times out after a few tries, displaying the results. It also pings them asynchronously, so pinging huge sets of hosts goes pretty fast. In a test here, I ran it on a 254 addresses in 35 seconds.

Read on for more details and examples of what fping can do.

For example, by default fping will read the hosts to ping either from the command-line or from stdin (one line per host):

guin:contrib$ echo 10.1.5.{1,2,3,4,5} | fmt -1 | fping is alive is alive
ICMP Host Unreachable from for ICMP Echo sent to
[...] is unreachable is unreachable is unreachable

Note that nmap can also ping hosts and read the list of hosts from stdin, with the command-line “nmap -sP -iL -”.

One thing I commonly do is to use the “-g” option to have fping generate all the addresses to ping: “fping -g”. This pings all 254 addresses in the specified block.

For a quicker ping you can use “-c 1” to only send one packet. Usually it will send several packets just to make sure that you get responses from all the hosts it can. For example: “fping -c 1 -g” takes only 10 seconds to run where without the “-c 1” it takes 35.

You can also use “-c” with a larger number, and combine it with “-q” (quiet) to get some nice, general statistics about the host's network reachability: “fping -c 5 -q” will result in the output “ : xmt/rcv/%loss = 5/5/0%, min/avg/max = 1.46/1.50/1.59”.

The “-q” option alone will simply set the process exit code as to whether the host is up. So if you want to make sure a machine is up before doing something, you could run “fping -q && $SOME_OTHER_COMMAND”.

There are also arguments allowing you to control how quickly retransmissions are sent, how many retries, and data display formats.

fping is a great tool that I've come to rely on.

comments powered by Disqus

Join our other satisfied clients. Contact us today.