Ruby: Parsing an IP List to Send to IpInfoDB

lock-binary-blue-620x390

The code below does the following:

  1. Read a list of IPs

  2. Send each line (IP) to the ipinfodb web service

  3. Parse the XML result

  4. Display it

  5. Write it to a CSV file for import into Excel

[ruby]require ‘rubygems’require ‘hpricot’require ‘open-uri’file = File.open(‘/Users/daniel/Development/newfile’, ‘w’)File.open(‘/Users/daniel/Development/ips’).each { |line| doc = Hpricot(open(“http://ipinfodb.com/ip_query.php?ip=#{line}”)) (doc/’response’).each do|el| ip = (el/’ip’).inner_html country = (el/’countryname’).inner_html state = (el/’regionname’).inner_html city = (el/’city’).inner_html puts “#{ip},#{country},#{state},#{city}” file.puts “#{ip},#{country},#{state},#{city}” file.flush end}[/ruby]

[ UPDATE: Improved the code. And here’s the output. The IPs were from my recent Apache logs. ]

124.115.0.170,China,Beijing,Beijing
209.85.238.23,United States,California,Mountain View
72.199.165.162,United States,California,Chula Vista
69.244.60.85,United States,Arizona,Tucson
58.61.164.40,China,Guangdong,Shenzhen
69.244.60.85,United States,Arizona,Tucson
58.61.164.40,China,Guangdong,Shenzhen
76.7.135.140,United States,Tennessee,Piney Flats
66.90.245.59,United States,Texas,Austin
196.25.255.210,South Africa,Gauteng,Pretoria
71.186.185.75,United States,New York,Buffalo
60.250.3.3,Taiwan,T'ai-pei,Taipei
207.67.117.170,United States,Minnesota,Saint Paul
68.211.221.104,United States,Georgia,Marietta
74.227.113.88,United States,North Carolina,Raleigh
89.151.84.34,United Kingdom,Reading,Reading
74.227.113.88,United States,North Carolina,Raleigh
66.249.67.109,United States,California,Mountain View
74.227.113.88,United States,North Carolina,Raleigh
66.249.67.109,United States,California,Mountain View
74.227.113.88,United States,North Carolina,Raleigh
75.101.212.153,United States,Washington,Seattle
8.12.144.17,United States,California,Sunnyvale
8.12.144.16,United States,California,Sunnyvale
8.12.144.58,United States,California,Sunnyvale
8.12.144.17,United States,California,Sunnyvale
66.249.67.109,United States,California,Mountain View
68.192.113.138,United States,New Jersey,Howell
87.194.122.211,United Kingdom,London,London
220.233.6.5,Australia,New South Wales,Sydney
87.194.122.211,United Kingdom,London,London
220.233.6.5,Australia,New South Wales,Sydney
209.86.226.20,United States,Kentucky,Louisville
66.108.116.122,United States,New York,New York
92.13.144.111,United Kingdom,Staffordshire,Tamworth
124.129.22.63,China,Shandong,Jinan
196.25.255.210,South Africa,Gauteng,Pretoria
67.239.178.194,United States,North Carolina,Kitty Hawk
92.13.144.111,United Kingdom,Staffordshire,Tamworth
75.101.136.193,United States,Washington,Seattle
66.108.116.122,United States,New York,New York
24.24.87.100,United States,New York,Vestal
67.239.178.194,United States,North Carolina,Kitty Hawk
124.195.13.118,Indonesia,Jakarta Raya,Jakarta
193.212.66.217,Norway,More og Romsdal,Kristiansund
173.70.158.106,United States,New Jersey,Bloomfield

[ Note: the IPs were uniq‘d before being read in. ]

Related posts: