#!/usr/local/bin/ruby require 'whois.rb' include Whois def hostaddr(host) Socket::gethostbyname(host)[3].unpack("C.C.C.C").join(".") end NTMPL = "host-add.tmpl" RMTMPL = "host-rm.tmpl" ONS = "^who\." NNS = "whine.osd.bsdi.com" # There must be a better way... NNSIP = hostaddr(NNS) CT = "Individual" TMP = "/tmp" # FIXME: get from env WHOST = "whois.internic.net" NSIHOST = "whois.networksolutions.com" TRLIST = [["EMAIL", "ea"], ["ONS", "ons"], ["ONSIP", "onsip"], ["OHH", "ohh"], ["NNS", "NNS"], ["NNSIP", "NNSIP"], ["NHH", "nhh"], ["CT", "CT"], ["CH", "ch"]] def push_val(o, k, v) if o.has_key? k then o[k] << v else o[k] = [v] end end def parse_whois(info) actual_object = {} tcf = 0 info.each do |i| if i =~ /^ (Administrative Contact, )?Technical Contact/ then tcf = 1 elsif tcf == 1 && i =~ /^ .*, .* \((.*)\) (.*@.*)$/ then push_val(actual_object, "Technical Contact", $1) push_val(actual_object, "Email Address", $2) tcf = 0 elsif i =~ /^\[[^]]*\] \((.*-HST)\)$/ push_val(actual_object, "Host Handle", $1) elsif i =~ /^ ([^:]+):\W*(.*)$/ then push_val(actual_object, $1, $2) end end if actual_object.empty? then [] else [actual_object] end end def check_val(v, s) if !v then $stderr.print "Could not get #{s} from whois information.\n" exit 1 end end def get_info(d) print "=> Getting whois info for #{d}\n" ns = nil ch = nil ea = nil q = query(d, WHOST, :parse_whois) q+= query("domain " + d, NSIHOST, :parse_whois) for w in q for k, v in w case k when "Name Server" for n in v if n =~ /#{ONS}/i then ns = n end end when "Technical Contact" ch = v when "Email Address" ea = v end end end check_val(ns, "name server") check_val(ch, "contact handle") check_val(ea, "email address") return [ns, ch, ea] end def get_host(h) print "=> Getting host handle for #{h}...\n" hh = nil ip = nil for w in query("host " + h, NSIHOST, :parse_whois) for k, v in w case k when "Host Handle" hh = v when "Address" ip = v end end end check_val(hh, "host handle") check_val(ip, "name server address") return [hh, ip] end for d in $* print "===> #{d}\n" i = get_info(d) ons = i[0] ch = i[1] ea = i[2] h = get_host(ons) ohh = h[0] onsip = h[1] # h = get_host(NNS) # nhh = h[0] nhh = "TEST-HST" for fn in [NTMPL, RMTMPL] ofn = fn.sub("host", d).sub(".tmpl", ".form") print "=> Writing form #{ofn} from template #{fn}...\n" inf = open(fn, 'r') outf = open(ofn, 'w') for l in inf for t in TRLIST l.gsub!(/%#{t[0]}%/, eval(t[1])) end outf.puts(l) end inf.close outf.close end end