Querying DNS Record Types (A, MX, CNAME, TXT, NS) via Domain with Ruby!


Uzman üye
14 Ara 2020



Hello, I'm Bunjo, in this topic I will explain what DNS is and how to retrieve DNS records in Ruby.


DNS (Domain Name Server)

DNS (Domain Name System) is a service used to access any resource on the internet. DNS translates domain names on the internet (for example: example.com) into IP addresses (for example: This allows users to use easier-to-remember domain names rather than complex IP addresses that they must remember when accessing websites, email servers, and other internet services.

The basic functions of DNS are:
Domain Name Resolution: DNS resolves domain names that users enter in their browsers or other applications to the IP addresses corresponding to that domain name. For example, when the user types "www.example.com", DNS translates that domain name to the correct IP address, providing access to the website.

Domain Forwarding: DNS can map a domain name to multiple IP addresses. This is important for situations such as load balancing for high-traffic websites or the use of backup servers.

Email Routing: DNS uses MX (Mail Exchange) records to facilitate email sending. These records determine which server incoming emails from a domain will be directed to.

Other Records: DNS may also contain other information about domain names, for example, CNAME (Canonical Name) records can point one domain to another, TXT (Text) records can provide additional information, and NS (Name Server) records can identify the domain's DNS servers .

DNS is distributed as a worldwide network, and millions of DNS servers come together to serve internet users. This ensures that the internet works reliably and quickly. However, DNS response times and reliability can affect internet access speed and user experience. Therefore, DNS service providers and internet service providers constantly work to maintain an effective DNS infrastructure.

Ref: DNS Nedir? Ne İşe Yarar? | Millenicom Blog


DNS Working Process

Resolution of the Domain Name:

The user enters a domain name into a browser (for example: "www.example.com").
The browser contacts a local DNS server (for example, a DNS server provided by the ISP) to resolve this domain name.

Local DNS Server Query:

The local DNS server checks its local cache to resolve the user's request.
If the requested record (for example, the IP address for "www.example.com") exists in the cache, it responds immediately.
If there are no records in the cache, the local DNS server moves up the DNS hierarchy, starting with the root DNS servers.

Root DNS Servers Query:

Since the local DNS server knows the IP addresses of the root DNS servers, it first sends a query to resolve the IP addresses of the root DNS servers.
Root DNS servers provide IP addresses of TLD (Top-Level Domain) servers, which contain information about top-level domain name extensions such as .com, .net, .org.

TLD Server Query:

The local DNS server sends a query to TLD servers to resolve IP addresses for top-level domain extensions such as .com, .net, .org.
TLD servers provide the IP addresses of authoritative nameservers for the requested domain name (for example, "example.com").

Authoritative Name Servers Query:

The local DNS server sends a query to the IP addresses given by the TLD to resolve the IP addresses of the domain's authoritative nameservers.
Authoritative name servers provide the IP address registered for the requested domain name.

Returning the Answer:

The local DNS server adds the IP address it receives to the cache and returns this IP address in response to the user.

The browser accesses the requested website with the IP address it receives.

Ref: https://www.cloudflare.com/tr-tr/learning/dns/what-is-dns/


What are DNS Records?

A (Address) Record: The A record is the basic DNS record that associates a domain name with its IPv4 address.
For example, if the domain name "www.example.com" is associated with the IPv4 address "", there is an A record that defines this association.

AAAA (IPv6 Address) Record: AAAA record is the record that associates a domain name with its IPv6 address. IPv6 is an alternative internet protocol to IPv4 and offers a larger address space.

CNAME (Canonical Name) Record: CNAME record is used when a domain name is directed to another domain name.
For example, there might be a CNAME record where the domain "www.example.com" points to the domain "example.com".

MX (Mail Exchange) Record: The MX record determines which server incoming emails from a domain will be directed to.
For example, it can be specified that e-mails coming to the "example.com" domain will be directed to the "mail.example.com" server.

TXT (Text) Record: A TXT record contains text information associated with a domain name.
This information is often used for verification, authentication or other special purposes.

NS (Name Server) Record: The NS record specifies the authoritative DNS servers of a domain name.
These records identify the authoritative servers that provide DNS management of the domain name.

PTR (Pointer) Record: PTR record allows an IP address to be associated with a domain name.
This is often used in reverse DNS queries and allows IP addresses to be converted into domain names.


Querying DNS Records in Ruby

require 'resolv'

domain = "example.com"

# A
a_records = Resolv::DNS.new.getresources(domain, Resolv::DNS::Resource::IN::A)
a_records.each do |record|
  puts "A Record: #{record.address}"

# MX
mx_records = Resolv::DNS.new.getresources(domain, Resolv::DNS::Resource::IN::MX)
mx_records.each do |record|
  puts "MX Record: #{record.exchange} (Priority: #{record.preference})"

cname_records = Resolv::DNS.new.getresources(domain, Resolv::DNS::Resource::IN::CNAME)
cname_records.each do |record|
  puts "CNAME Record: #{record.name}"

txt_records = Resolv::DNS.new.getresources(domain, Resolv::DNS::Resource::IN::TXT)
txt_records.each do |record|
  puts "TXT Record: #{record.strings.join(' ')}"

# NS
ns_records = Resolv::DNS.new.getresources(domain, Resolv::DNS::Resource::IN::NS)
ns_records.each do |record|
  puts "NS Record: #{record.name}"

First, to use the resolv library, we call the library with the require 'resolv' statement.

Then, we assign a domain name such as example.com to the domain variable. This represents the domain name whose DNS records we will query.

The following block uses the getresources method of the Resolv:: DNS class to retrieve A records. This method queries DNS for the specified domain name and record type (IN::A) and assigns the results to the a_records variable. Then, we loop over these records and print each A record to the screen.

Similarly, the same process steps are followed for MX records, CNAME records, TXT records and NS records. The getresources method is used for each record type, the results are assigned to the relevant variables, and then the relevant information for each record type is printed to the screen using the loop.


Turkhackteam.org internet sitesi 5651 sayılı kanun’un 2. maddesinin 1. fıkrasının m) bendi ile aynı kanunun 5. maddesi kapsamında "Yer Sağlayıcı" konumundadır. İçerikler ön onay olmaksızın tamamen kullanıcılar tarafından oluşturulmaktadır. Turkhackteam.org; Yer sağlayıcı olarak, kullanıcılar tarafından oluşturulan içeriği ya da hukuka aykırı paylaşımı kontrol etmekle ya da araştırmakla yükümlü değildir. Türkhackteam saldırı timleri Türk sitelerine hiçbir zararlı faaliyette bulunmaz. Türkhackteam üyelerinin yaptığı bireysel hack faaliyetlerinden Türkhackteam sorumlu değildir. Sitelerinize Türkhackteam ismi kullanılarak hack faaliyetinde bulunulursa, site-sunucu erişim loglarından bu faaliyeti gerçekleştiren ip adresini tespit edip diğer kanıtlarla birlikte savcılığa suç duyurusunda bulununuz.