#!/usr/bin/env ruby
require 'rubygems'
require 'httparty'
require 'rest_client'
require 'net/dns/resolver'
require 'google_spreadsheet'
# Define ARIN class and set it's base URL
class ARIN
include HTTParty
base_uri 'http://whois.arin.net/rest'
# Remove http:// and / from a string
def self.only_uri(uri)
uri.gsub(/http:\/\/(.*?)\/$/,"\\1")
end
def self.only_orghandle(ip)
# Ask ARIN which ORG handle owns the IP address passed to this method
get("/ip/#{ip}").parsed_response["net"]["orgRef"].to_s.split('/').last
end
def self.only_asn(orghandle)
# Ask ARIN which ASN owns the orghandle passed to this method
get("/org/#{orghandle}/asns").parsed_response["asns"]["asnRef"].to_s.split('/').last
end
# Return a hash of all networks owned by a given orghandle
def self.only_nets(orghandle)
get("/org/#{orghandle}/nets").parsed_response["nets"]["netRef"]
end
end
# Login to Google Spreadsheet
session = GoogleSpreadsheet.login("email@address.com", "password")
# Establish the spreadsheet session
ws = session.spreadsheet_by_key("sessionid").worksheets[0]
# Iterate through the spreadsheet, starting at the second row to avoid the header
for row in 2..ws.num_rows
hostname = ARIN.only_uri(ws[row,2])
hostname.each do |hostname|
begin
Net::DNS::Resolver.new(:nameservers => "8.8.4.4")
Net::DNS::Resolver.start(hostname).each_address do |ip|
orghandle = ARIN.only_orghandle(ip)
p orghandle
asn = ARIN.only_asn(orghandle)
ws[row,21] = asn
p "#{hostname}'s ip address is #{ip}, it's ORG Handle is #{orghandle}, and it's asn is #{asn}"
p ARIN.only_nets(orghandle)
end
rescue
$stderr.print "DNS Shit failed: " + $!
end
end
end
# Write changes to worksheet
ws.save()
Refactorings
No refactoring yet !
Krzysztof Wilczynski
January 9, 2011, January 09, 2011 00:20, permalink
There is also this http://www.ruby-whois.org/ for those who want a little bit more generic whois query tool.
joshua montross
January 13, 2011, January 13, 2011 20:59, permalink
you could put the second set of code into a class, use the initialize method, put that loop into another method, and then you can require this file in another application and it would run with the last two lines. I couldn't test because I don't have the two files at the bottom of your require statement. I refactored the Net::DNS::Resolver line to just be called net. I don't think this breaks it. Also, I used class variable @ws in the initialize method and then again in the fill_spreadsheet method. Might make sense to make save a diff method and only call it if it makes doesn't break on that begin rescue block.
#!/usr/bin/env ruby
require 'rubygems'
require 'httparty'
require 'rest_client'
# I don't have so can't test
require 'net/dns/resolver'
# this must be a file
require 'google_spreadsheet'
# Define ARIN class and set it's base URL
class ARIN
include HTTParty
base_uri 'http://whois.arin.net/rest'
# Remove http:// and / from a string
def self.only_uri(uri)
uri.gsub(/http:\/\/(.*?)\/$/,"\\1")
end
def self.only_orghandle(ip)
# Ask ARIN which ORG handle owns the IP address passed to this method
get("/ip/#{ip}").parsed_response["net"]["orgRef"].to_s.split('/').last
end
def self.only_asn(orghandle)
# Ask ARIN which ASN owns the orghandle passed to this method
get("/org/#{orghandle}/asns").parsed_response["asns"]["asnRef"].to_s.split('/').last
end
# Return a hash of all networks owned by a given orghandle
def self.only_nets(orghandle)
get("/org/#{orghandle}/nets").parsed_response["nets"]["netRef"]
end
end
class Noob
include ARIN
def initialize user pass
# Login to Google Spreadsheet
session = GoogleSpreadsheet.login(user, pass)
# Establish the spreadsheet session
@ws = session.spreadsheet_by_key("sessionid").worksheets[0]
end
def fill_spreadsheet
# Iterate through the spreadsheet, starting at the second row to avoid the header
for row in 2..@ws.num_rows
hostname = ARIN.only_uri(ws[row,2])
hostname.each do |hostname|
begin
net = Net::DNS::Resolver.new(:nameservers => "8.8.4.4")
net.start(hostname).each_address do |ip|
orghandle = ARIN.only_orghandle(ip)
p orghandle
asn = ARIN.only_asn(orghandle)
ws[row,21] = asn
p "#{hostname}'s ip address is #{ip}, it's ORG Handle is #{orghandle}, and it's asn is #{asn}"
p ARIN.only_nets(orghandle)
end
rescue
$stderr.print "DNS Shit failed: " + $!
end
end
end
@ws.save
end
end
# make worksheet
goog_ws = Noob.new("username","password")
# fill it in and write it
goog_ws.fill_spreadsheet
Err, forgive the n00bness, objects are new to me.