A Simple Script for Harvesting DNS, Country, State, and City Information From a List of IP Addresses

My buddy at work asked me if I could find some location information for a list of IPs. I knew of the GeoIP / GeoLite project(s), so I said yes and then proceeded to put together the following quick hack in bash.

Here’s what it does:

Pull a list of IP addresses from your apache logs (you can get the list from anywhere, of course).

Strip the duplicates (using uniq)

Use host to get the DNS entry for the IP

Use the default geoiplookup to get the country for the IP.

Use geoiplookup with the city file passed to it to get the city (and other info) for the IP.

Output the whole thing into a .csv file that will import instantly into Excel.

Setup

The GeoLiteCity.dat file manually, which you need to put somewhere. I put it next to the default one that comes with geoip, which is in /usr/share/GeoIP/.

ensure the paths in your environment match the paths in the script.

Of course, if I were really cool I’d use a real programming language and one of the APIs, but this is quick, dirty and effective. I’m thinking about building a rails-based web service for doing it. If anyone’s interested or has any comments on this one, let me know in the comments or send me a mail at daniel@danielmiessler.com. ::