# # Copyright (c) 2000 by Michael Neumann (neumann@s-direktnet.de) # # 02.01.2000 # require "socket" module Whois INFO = [ # domain-prefix-array, whois-host, parse-method, option_suffix, option_prefix [ [:de, :fr, :uk, :es], "whois.ripe.net", :parse_ripe, "", "" ], [ [:com, :net, :org, :edu], "whois.internic.net", :parse_internic, "", ""], [ [:jp], "whois.nic.ad.jp", :parse_jpnic, "", "/e"] ] # queries domains def query_domain (domain_part, whois_arr=nil) if whois_arr == nil then whois_arr = get_whois_for_domainprefix (domain_prefix(domain_part)) end query_any(domain_part, whois_arr) end # queries domains, ip's, handles etc... def query_any (query_string, whois_arr=INFO) case whois_arr when String then whois_arr = get_whois_for_domainprefix (whois_arr) when Integer then whois_arr = get_whois_for_domainprefix (whois_arr.id2name) end ret = [] whois_arr.each {|i| result = query(i[3] + query_string + i[4], i[1], i[2]) if ! result.empty? then ret += result end } ret end def query (query_string, whois_host, parse_method) inf = raw_whois (query_string, whois_host) return eval(parse_method.id2name + " inf") end def raw_whois (send_string, host) s = TCPsocket.open(host, 43) s.write(send_string+"\n") ret = "" while s.gets do ret += $_ end s.close return ret end # returns all whois-hosts which are registered for the domain-prefix def get_whois_for_domainprefix (prefix) INFO.select {|i| has_prefix? i, prefix } end private def domain_prefix (dom) dom =~ /.*\.([^\.]+)$/ $1 end def has_prefix? (i, prefix) i[0].each {|i| if i.id2name.downcase == prefix.downcase then return true end } return false end def parse_jpnic (inf) # whois.nic.ad.jp, japanisch actual_object = {} inf.each do |i| if i =~ /^[^\[]*\[([^ ][^\]]+)\]\W*(.*)$/ then if $1[0] != " " then if actual_object.has_key? $1 then actual_object[$1] << $2 else actual_object[$1] = [$2] end end end end if actual_object.empty? then [] else [actual_object] end end def parse_ripe (inf) # whois.ripe.net, europäische Domains all_objects = [] actual_object = {} is_code = false inf.each do |i| if i == "\n" or i =~ /^%.*$/ then if is_code then all_objects << actual_object actual_object = {} is_code = false end else is_code = true i =~ /^([^:]+):\W*(.*)$/ if actual_object.has_key? $1 then actual_object[$1] << $2 else actual_object[$1] = [$2] end end end all_objects end def parse_internic (inf) # whois.internic.net, com net org edu actual_object = {} inf.each do |i| if i =~ /^ ([^:]+):\W*(.*)$/ if actual_object.has_key? $1 then actual_object[$1] << $2 else actual_object[$1] = [$2] end end end if actual_object.empty? then [] else [actual_object] end end end def print_query (dom) query_domain(dom).each {|i| i.each {|key,value| print key,"=>"; p value} print "\n\n" readline } end if __FILE__ == $0 include Whois print_query("netlab.co.jp") print_query("ruby-lang.org") print raw_whois("ruby-lang.org","whois.discount-domain.com") readline end