1 #################################################################
3 # ----------------------------
4 # by Chris Gahan (chris@ill-logic.com)
8 # Lets you lookup the owner and their address for any IP address
11 #################################################################
16 #################################################################
17 ## ARIN Whois module...
24 keys.grep(/^(City|Address|StateProv|(Org|Cust)Name)$/).any?
28 keys.grep(/^(CIDR|NetHandle|Parent)$/).any?
32 keys.grep(/^(R|Org)(Tech|Abuse)(Handle|Name|Phone|Email)$/).any?
36 customer? or network? or contact?
40 self[keys.grep(/^(Org|Cust)Name$/).first]
44 [ self['City'], self['StateProv'], self['Country'] ].compact.join(', ')
48 [ self['Address'], location, self['PostalCode'] ].compact.join(', ')
59 def split_array_at(a, &block)
60 return a unless a.any?
66 a.each_with_index do |el,i|
69 results << a[last_cutpoint...i]
75 if last_cutpoint < a.size or last_cutpoint == 0
76 results << a[last_cutpoint..-1]
83 # ------------------------
92 # Network Information:
93 # CIDR (69.195.25.0/25)
94 # NetHandle (NET-72-14-192-0-1)
95 # Parent (NET-72-0-0-0-0)
98 # ({R,Org}{Tech,Abuse}{Handle,Name,Phone,Email})*
101 return if @data =~ /^No match found /
102 chunks = @data.gsub(/^# ARIN WHOIS database, last updated.+/m, '').scan(/(([^\n]+\n)+\n)/m)
103 chunks.map do |chunk|
106 chunk[0].scan(/([A-Za-z]+?):(.*)/).each do |tuple|
108 result[tuple[0]] = tuple[1].empty? ? nil : tuple[1]
117 return unless chunks = parse_chunks
119 results = split_array_at(parse_chunks) {|chunk|chunk.customer?}
120 results.map do |chunks|
122 :customer => chunks.select{|x|x.customer?}[0],
123 :net => chunks.select{|x|x.network?}[0],
124 :contacts => chunks.select{|x|x.contact?}
129 # Return a hash with :customer, :net, and :contacts info filled in.
130 def get_most_specific_owner
131 return unless datas = get_parsed_data
133 datas_with_bitmasks = datas.map do |data|
134 bitmask = data[:net]['CIDR'].split('/')[1].to_i
137 #datas_with_bitmasks.sort.each{|x|puts x[0]}
138 winner = datas_with_bitmasks.sort[-1][1]
141 end # of class ArinWhoisParser
145 def raw_whois(query_string, host)
146 s = TCPsocket.open(host, 43)
147 s.write(query_string+"\n")
154 data = raw_whois("+#{ip}", 'whois.arin.net')
155 arin = ArinWhoisParser.new data
156 arin.get_most_specific_owner
159 def lookup_location(ip)
161 result[:customer].location
164 def lookup_address(ip)
166 result[:customer].address
170 if result = lookup(ip)
171 "#{result[:net]['CIDR']} => #{result[:customer].owner} (#{result[:customer].address})"
181 #################################################################
185 class IPLookupPlugin < Plugin
186 def help(plugin, topic="")
187 "iplookup [ip address / domain name] => lookup info about the owner of the IP address from the ARIN whois database"
190 def iplookup(m, params)
194 ip = Resolv.getaddress(params[:domain])
195 reply += "#{params[:domain]} | "
197 m.reply "#{e.message}"
204 reply += ArinWhois.lookup_info(ip)
208 def userip(m, params)
209 #users = @channels[m.channel].users
210 #m.reply "users = #{users.inspect}"
211 #m.reply @bot.sendq("WHO #{params[:user]}")
216 plugin = IPLookupPlugin.new
217 plugin.map 'iplookup :ip', :action => 'iplookup', :requirements => {:ip => /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/}
218 plugin.map 'iplookup :domain', :action => 'iplookup', :requirements => {:domain => /^[a-z0-9\.\-]{4,255}$/i}
219 plugin.map 'userip :user', :action => 'userip', :requirements => {:user => /\w+/}
224 data = open('whoistest.txt').read
225 c = ArinWhoisParser.new data
226 puts c.get_parsed_data.inspect