From e69ebabc5f347b7af284daa50e2bbd0c52fb965a Mon Sep 17 00:00:00 2001 From: Dan Buch Date: Mon, 25 Jul 2011 22:36:02 -0400 Subject: [PATCH] working through 14.4 --- .../04-list-a-to-z-domain-addrs-concurrent.rb | 29 +++++++++++++++++++ cookbook/014/04-list-oreilly-mx-ns.rb | 11 +++++++ cookbook/014/04.rb | 3 ++ 3 files changed, 43 insertions(+) create mode 100644 cookbook/014/04-list-a-to-z-domain-addrs-concurrent.rb create mode 100644 cookbook/014/04-list-oreilly-mx-ns.rb create mode 100644 cookbook/014/04.rb diff --git a/cookbook/014/04-list-a-to-z-domain-addrs-concurrent.rb b/cookbook/014/04-list-a-to-z-domain-addrs-concurrent.rb new file mode 100644 index 0000000..6065188 --- /dev/null +++ b/cookbook/014/04-list-a-to-z-domain-addrs-concurrent.rb @@ -0,0 +1,29 @@ +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 diff --git a/cookbook/014/04-list-oreilly-mx-ns.rb b/cookbook/014/04-list-oreilly-mx-ns.rb new file mode 100644 index 0000000..25a91bd --- /dev/null +++ b/cookbook/014/04-list-oreilly-mx-ns.rb @@ -0,0 +1,11 @@ +require 'resolv' + +dns = Resolv::DNS.new +domain = "oreilly.com" +dns.each_resource(domain, Resolv::DNS::Resource::IN::MX) do |mail_server| + puts mail_server.exchange +end + +dns.each_resource(domain, Resolv::DNS::Resource::IN::NS) do |nameserver| + puts nameserver.name +end diff --git a/cookbook/014/04.rb b/cookbook/014/04.rb new file mode 100644 index 0000000..93b1e3c --- /dev/null +++ b/cookbook/014/04.rb @@ -0,0 +1,3 @@ +require 'resolv' + +Resolv::DNS.new.each_address('oreilly.com') { |addr| puts addr }