tags:

views:

182

answers:

4

I need to get the txt field from the DNS record. Is there any ruby api to do something like this?

nslookup -q=txt xxxx.com

Thanks

+1  A: 

Try the Net::DNS gem. Scroll down on that page for installation and usage instructions.

calmh
i tried using Net::DNS::Resolver.start("waycoolblog.com", Net::DNS::TXT). but i count get the value for the txt field. how should i be actually using it??
railscoder
I think you're using it correctly, but it seems broken and doesn't actually return the TXT record as it should. Sorry.
calmh
A: 

Or use system("nslookup -q=txt xxxx.com")

TweeKane
am thinking of not using with system() as it might be very expensive.
railscoder
+2  A: 

Try installing the dnsruby gem.

The code is actively maintained, and used in some significant production systems.

require 'rubygems'
require 'dnsruby'
include Dnsruby

# Use the system configured nameservers to run a query
res = Dnsruby::Resolver.new
ret = res.query("google.com", Types.TXT)
print ret.answer

(Code tested on MacOS X - prints the Google SPF record)

See also @Alex's answer which is more idiomatic Ruby - Alex is the author of dnsruby.

Alnitak
Thanks, it works perfect.
railscoder
+1  A: 
require 'dnsruby'
Dnsruby::DNS.open {|dns|
  dns.each_resource("google.com", "TXT") {|rr| print rr}
    # or
  print dns.getresource("google.com", "TXT")}
}
Alex