anusuya anusuya - 1 year ago 59
Ruby Question

Getting a DNS TXT record in Ruby

I need to get the txt field from the DNS record.

Is there any ruby api to do something like this?

nslookup -q=txt

Answer Source

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 =
ret = res.query("", 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.