Create a gist now

Instantly share code, notes, and snippets.

@haqu /lip.rb
Created Feb 21, 2011

IP Location on Google Maps
#!/usr/bin/env ruby -w
# Example of usage:
# $ ./lip.rb snippets.dzone.com
# Hostname: snippets.dzone.com
# Country Code: US
# Country Name: United States
# Region: CA
# Region Name: California
# City: Los Angeles
# Postal Code: 90017
# Latitude: 34.0530
# Longitude: -118.2642
# ISP: CoreExpress
# Organization: CoreExpress
# Metro Code: 803
# Area Code: 213
# Google Maps URL: http://maps.google.com/maps?q=34.0530,+-118.2642&iwloc=A&hl=en
if ARGV.empty?
puts <<-T
Locate IP by haqu
usage: ./lip.rb <ip or domain> ...
T
exit
end
require 'net/http'
require 'uri'
uri = URI.parse('http://www.maxmind.com/app/locate_ip')
res = Net::HTTP.post_form(uri,
{ 'ips' => ARGV.join(' '),
'type' => '', 'u' => '', 'p' => ''
} )
fstr = res.body
fstr.gsub!("Edition Results<\/span><p>","CHECKPOINT")
fstr =~ /CHECKPOINT(.+?)<\/table>/m
fields = $1.grep(/<(th|td)>/)
fields.each do |f|
f.strip!
f.gsub!(/<[^>]+>/,"")
end
(0...13).each do |i|
puts ". #{fields[i]}: #{fields[i+13]}"
end
maplink = "http://maps.google.com/maps?q=#{fields[20]},+#{fields[21]}&iwloc=A&hl=en"
puts ". Google Maps URL: #{maplink}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment