DNS (Domain Name System) is ubiquitous to any website or application operating over the internet, as people find it easier to remember the website by the domain names rather than their IP addresses. The DNS system can store data for a variety of services for a domain like MX (Mail Exchanger record) which specifies the servers that'd be handling the mails for this domain, SRV (Service record) which holds the definitions for the hostnames & ports for specific services for a specific domain, and most import & frequently used is the A (Address record) which host the destination IP address for any hostname. In this article we will see how to query DNS servers for various DNS records using Ruby, for which we'll be look at 2 Ruby gems namely Resolv & dnsruby. Querying using Resolv Resolv is a thread-safe Ruby gem which can query DNS for all known record types, let us look at the code snippet below to get a working idea of the gem & it's methods. Code: require 'resolv-idn' p Resolv.getaddress "www.go4expert.com" # create an object dns_obj = Resolv::DNS.new # alternately, you can specify your own DNS servers to query # instead of the system default dns_obj = Resolv::DNS.new( :nameserver => ['8.8.8.8', '8.8.4.4'] ) # now let's get the MX record for go4expert.com res = dns_obj.getresources "go4expert.com", Resolv::DNS::Resource::IN::MX # loop through the response p res.map { |r| [r.exchange.to_s, r.preference] } # get TXT records for google.com res = dns_obj.getresources "google.com", Resolv::DNS::Resource::IN::TXT # loop through the response p res.map { |r| r.data.to_s } Querying using dnsruby Gem DNS Ruby is very similar to Resolv, but it gives a complete DNS implementation including DNSSEC (Domain Name System Security Extensions) using which you can validate the response so that the response can be authenticated. Code: require 'dnsruby' ## import constants into the namespace include Dnsruby # use the system configured nameservers by default dns = Dnsruby::Resolver.new res = dns.query("google.com", Types.TXT) p res.answer Dnsruby::DNS.open {|dns| dns.each_resource("google.com", Types.A) {|r| p r} } I hope this article helps you to power your applications with ability to query DNS servers.