box-o-sand/RubyFun/cookbook/014/04-list-a-to-z-domain-addrs-concurrent.rb
Dan Buch 0a9428093c Add 'RubyFun/' from commit 'b01c6826131196ba58b5288a3182f2526c89c249'
git-subtree-dir: RubyFun
git-subtree-mainline: a04a502787
git-subtree-split: b01c682613
2013-01-09 23:50:14 -05:00

30 lines
702 B
Ruby

require 'resolv'
require 'resolv-replace'
def multiple_lookup(*names)
dns = Resolv::DNS.new
results = {}
threads = []
names.each do |name|
threads << Thread.new(name) do |name|
begin
dns.each_address(name) { |a| (results[name] ||= []) << a }
rescue Resolv::ResolvError
results[name] = nil
end
end
end
threads.each { |t| t.join }
return results
end
domains = ("a".."z").collect { |l| l + '.com' }
multiple_lookup(*domains).sort.each do |name, addresses|
if addresses
puts "#{name}: #{addresses.size} " + \
"address#{addresses.size == 1 ? "" : "es"}"
end
end