Wednesday, May 16, 2012

Ping multiple hostnames or IPs from an input file

So my problem was that I needed to ping a list of hostnames from an input file to see if they were alive. All the network scanning programs I usually use accept an input file but the file must contain IP addresses and not hostnames. Lame.

So I got on my Fedora 16 Linux box and tried fping which I hadn't looked at in a while and it was even more mega-cool than I thought. fping is a command-line Linux tool and will accept an input file with hostnames or IPs or a mix of both. It will do lots of other things, too, like resolve the hostnames to IPs in the output, etc. and works great for scripts. Take a look at the man page for all kinds of coolness. fping is also very fast because it pings the hosts in parallel instead of one at a time.

Go ahead and install it and give it a whirl. On a Red Hat-based OS like mine, you can do this with:

sudo yum install fping

Below is a command that you can use to send your input text file to fping and have it send all the output to a file:

sudo fping -f scaninput.txt > scanoutput.txt 2>&1

Replace scaninput.txt with your input file and replace scanoutput.txt with your desired output filename. For my input file, I just took a list of hostnames from a spreadsheet and pasted them into a text file. The -f option is used to specify the input filename. The > of course sends the output to a file. The 2>&1 catches any error output (like a failed ping) that was originally only going to be printed on the screen and makes sure it gets sent to the output file as well. That's because a 2> redirect is different than a normal redirect. It redirects STDERR (standard error) output instead of the normal STDOUT (standard out). In our case, we are additionally sending the STDERR to STDOUT because STDOUT (default) is represented as 1 and STDERR as 2. Since STDOUT is already opened by the shell, STDERR will be appended to STDOUT so only a > is needed and not >> which would normally be used for appending. If you'd like to geek out on this epically, you can head over to http://www.linuxsa.org.au/tips/io-redirection.html and make your head hurt a little.


So after I got my scanoutput.txt I opened it in Excel for some cleaning up. (Hey, Microsoft Office is a decent product and my LibreOffice crashes like crazy so get off my back already!!) I used some Text to columns magic and got things looking nicely for continuing my project. Only downside was that I did have some duplicates of this nature:

[hostname1] is unreachable
ICMP Host Unreachable from [x.x.x.x] for ICMP Echo sent to [hostname1] ([x.x.x.x])


Fooling around with my fping redirection did not solve this because fping apparently likes to print the output twice for unreachables even if you don't do the redirection I've specified. No problem, though. After verifying that these were indeed duplicate lines, I just sorted the column alphabetically and removed all the rows that began with ICMP Host Unreachable which took all of 10 seconds. If anybody has a better way to do this, feel free to comment.

In the future, when I need to ping a list of hostnames from an input file fping will be my tool of choice. Enjoy!

2 comments:

  1. Anonymous,

    Nice screenshot. ;)

    Fping isn't meant to be run by simply typing "fping" and pressing enter. Read the man page by typing "man fping". At the top of the man page it says:

    "SYNOPSIS

    fping [ options ] [ systems... ]"

    This translates to mean that you need to type "fping [ options ] [ systems... ]", of course replacing "[ options ]" with your desired options listed in the man page and "[ systems... ]" with hostname(s)/IP(s) separated by a space.

    Hope that helps.

    And don't ever ask your computer to kill you. One day it may take you up on it. ;)

    -James

    P.S. Man pages are your friend!

    ReplyDelete

Spammy/foul language comments or those with an explicit avatar will be tossed in a 55 gallon drum and a match thrown in after them. (Oooo, now I can warm my hands!!)