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
TXT records are a type of Domain Name System (DNS) record that contains text information for sources outside of your domain. You add these records to your domain settings. You can use TXT records for various purposes. Google uses them to verify domain ownership and to ensure email security.
Use the Ruby stdlib Resolv::DNS
library without installing a gem:
require 'resolv'
txt = Resolv::DNS.open do |dns|
records = dns.getresources("_dmarc.yahoo.com", Resolv::DNS::Resource::IN::TXT)
records.empty? ? nil : records.map(&:data).join(" ")
end
#=> "v=DMARC1; p=reject; sp=none; pct=100; rua=mailto:[email protected], mailto:[email protected];"
getresources
returns an array of instances of the requested record class name (Resolv::DNS::Resource::IN::TXT
). Here, I return nil if the TXT record(s) or the host name were not found, otherwise I map over the records, call data
to get the values, then join them together.
Any DNS record type [TXT, NS, CNAME, MX, ...] can be queried as well by replacing TXT in the above example.
TXT records are "unstructured" and used for enhanced data for the hostname such as SPF, DKIM, DMARC configurations. In practice, there may be only one TXT record, but the RFC doesn't say how many there can be.
Read the docs at: http://www.ruby-doc.org/stdlib-2.1.1/libdoc/resolv/rdoc/index.html
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With