1. Computing
Send to a Friend via Email

Spotlight on RubyGems: GeoIP

Using GeoIP to Find Physical Locations

By

How many times have you wished you could figure out where a certain IP address was physically located? With the release of the GeoIP RubyGem, there's now an easy interface for searching GeoIP databases in Ruby. The information in the databases can be used to match IP addresses with physical locations. For a fee, the databases can be obtained from several companies, but there are also "demo" versions of the databases available that provide only country information (as opposed to full addresses).

Installing the Gem

Installing the GeoIP gem is the same as installing any other Ruby Gem.

$ sudo gem install geoip
Bulk updating Gem source index for: http://gems.rubyforge.org/
Successfully installed geoip-0.8.0
1 gem installed
Installing ri documentation for geoip-0.8.0...
Installing RDoc documentation for geoip-0.8.0...

Installing the GeoIP Database

Keep in mind that installing the gem doesn't install any GeoIP databases, it merely installs the interface used to search them. A free GeoIP database--with country information only--can be obtained from www.maxmind.com. It's a good place to to start, but if you want more detailed information, you can purchase a subscription to one of the GeoIP Databases offered by MaxMind.

Note that these databases don't have a long shelf life. ISPs change, IP addresses change hands and networks restructure. If you let a database get too old, much of the information may be out of date and incorrect. So, if you're going to rely on this information, it's best to purchase a subscription in order to get updated databases.

You also can download the GeoLite Country database for free from MaxMind. Download the Binary Format database (the filename is GeoIP.dat.gz) and uncompress it using the gunzip command or a GUI archive manager such as WinZip or WinRar.

$ wget -q http://www.maxmind.com/download/geoip/database/GeoIP.dat.gz
 $ gunzip GeoIP.dat.gz
 

Looking up Countries

Once the GeoIP database is obtained, you can start looking up IP addresses. To test it out, fire up irb in the same directory as the GeoIP database (or pass the full path to GeoIP.new) and create a new GeoIP object. Call the country method on that GeoIP object to look up the country of any hostname or IP address.

$ irb -rubygems -rgeoip
 irb(main):001:0> g = GeoIP.new('GeoIP.dat')
 ..snip..
 irb(main):002:0> g.country 'ruby.about.com'
 => ["ruby.about.com", "207.126.123.20", 225, "US", "USA", "United States", "NA"]
 irb(main):003:0> g.country 'geoip.rubyforge.org'
 => ["geoip.rubyforge.org", "205.234.109.19", 225, "US", "USA", "United States", "NA"]
 irb(main):004:0> g.country 'ruby-lang.org'
 => ["ruby-lang.org", "221.186.184.68", 111, "JP", "JPN", "Japan", "AS"]
 

As you can see, the country method returns an array with various information. Among them is the hostname and IP address, the 2 and 3 letter country code, the country ID number and the name of the country.

Looking up Other Information

Though the free GeoLite database doesn't contain any data other than the country data, if you subscribe to the more detailed databases you can get more information. You can, for example, get information about the city and ISP. The pattern for looking up this information is the same as with the country method, only you'll need to use the city and isp methods instead of the country method.

  1. About.com
  2. Computing
  3. Ruby
  4. Gems
  5. Spotlight on Gems: Geoip

©2014 About.com. All rights reserved.