Forums | Admin

Discussion Forums: open-discussion

Start New Thread Start New Thread

 

By: Alex D
RE: Examples of how to get values from lookups [ reply ]  
2012-02-07 15:55
I'm sorry you had difficulties finding adequate documentation - I'll try to find the time to update it soon.

In the meantime, thanks for posting this!


Alex.

By: Don Langham
Examples of how to get values from lookups [ reply ]  
2012-02-05 19:03
I had difficulty finding examples of how to do some seemingly basic yet important tasks with dnsruby. I needed to write a script that would return the IP address of a host name, for example. It's easily done, but none of the examples I found online demonstrated how to do this, or how, for another example, to get just the names of a domain's mail exchangers (without all the other information included in the resource record).

What follows is a script I wrote to demonstrates how to do both of these tasks. Specifically, it looks up the MX records for a domain and prints the full text of the response message, the names of the mail exchanger servers, and the IP address(es) for each exchanger host.

Note that the "rdata" attribute of the message is your friend when it comes to getting an IP address from a host name lookup. Likewise, the "exchange" attribute is what gets you the server name from an MX record message.

require 'rubygems'
require 'dnsruby'
include Dnsruby

# Initialize the dnsruby resolver class, providing the IP address
# of the name server. The IP address of the name server is optional,
# but I prefer to be explicit about the name server, including using
# the IP address rather than the host name.

resolver = Dnsruby::Resolver.new("8.8.8.8")

# Query for the MX records for the domain 'google.com'
googlemx = resolver.query("google.com",Types.MX)

print "-------- Here is the response message for MX record lookup --------\n"
# Output the text of the response message.
# This will look a lot like the text of "dig mx google.com @8.8.8.8" on a Linux command line
puts googlemx
print "\n\n"

print "-------- Mail exchanger host names from the answer section --------\n"
# You can use the "exhcange" attribute of the MX record answer to get the
# names of the mail exchangers.
# Use the each_answer method to process each record found in the
# answer section.
googlemx.each_answer{|mxrecord| print mxrecord.exchange.to_s + "\n"}
print "\n\n"

print "-----------MX exchanger name and IP address--------------\n"
#For each host exchanger, do an A record lookup and output all IPs
googlemx.each_answer{|mxrecord| resolver.query(mxrecord.exchange,Types.A).each_answer{
|arecord| print mxrecord.exchange.to_s + ", " + arecord.rdata.to_s + "\n"}
}
print "\n\n"

print "----------Same output as above, but using nested block calls-----------\n"
# Here's how to do all the lookups shown above as nested "each_answer" blocks
resolver.query("google.com",Types.MX).each_answer{
|mxrecord| resolver.query(mxrecord.exchange,Types.A).each_answer{
|arecord| print mxrecord.exchange.to_s + ", " + arecord.rdata.to_s + "\n"
}
}