DNS Module
The DNS module provides functions to test the performance and availability of DNS servers. This module is useful for DNS infrastructure monitoring, diagnosing name resolution issues, and measuring latency in DNS queries.
Available Functions
Section titled “Available Functions”1. dns.ping(server, domain)
Section titled “1. dns.ping(server, domain)”Performs a DNS query to a specific server and measures the response time (RTT - Round Trip Time).
Parameters
Section titled “Parameters”-
server (string): Address of the DNS server to test (can be an IP or domain name)
-
domain (string): Domain to be queried on the DNS server
Return
Section titled “Return”- number: Response time in milliseconds (DNS latency)
Behavior
Section titled “Behavior”-
Resolves the DNS server address (if it’s a domain name)
-
Connects to the DNS server on port 53 (UDP)
-
Performs an A record query for the specified domain
-
Measures the time between sending the query and receiving the response
-
Returns the time in milliseconds
Usage Example
Section titled “Usage Example”-- Test Google DNS latencylocal latencia = dns.ping("8.8.8.8", "google.com")-- latencia = 25 (example: 25 milliseconds)
-- Test Cloudflare public DNSlocal latencia_cf = dns.ping("1.1.1.1", "github.com")-- latencia_cf = 30 (example)
-- Test DNS server by namelocal latencia_local = dns.ping("dns.local", "servidor.producao")-- Tests the internal DNS server "dns.local"
-- Compare multiple DNS serverslocal servidores_dns = { {nome = "Google DNS", endereco = "8.8.8.8"}, {nome = "Cloudflare", endereco = "1.1.1.1"}, {nome = "Quad9", endereco = "9.9.9.9"}, {nome = "DNS Local", endereco = "192.168.1.1"}}
for _, dns in ipairs(servidores_dns) do local latencia = dns.ping(dns.endereco, "exemplo.com") print(dns.nome .. ": " .. latencia .. "ms")endAdditional Information
Section titled “Additional Information”Automatic DNS Server Resolution
Section titled “Automatic DNS Server Resolution”The dns.ping function automatically resolves the DNS server name if provided as a domain:
-- Works with IPdns.ping("8.8.8.8", "google.com")
-- Works with domain name (will be resolved first)dns.ping("dns.google", "exemplo.com")dns.ping("one.one.one.one", "exemplo.com") -- Cloudflare DNSA Record Query
Section titled “A Record Query”The function always performs A record (IPv4) queries.
UDP Protocol
Section titled “UDP Protocol”Queries are performed via UDP on port 53, which is the standard protocol for DNS queries.
Usage Example
Section titled “Usage Example”DNS Propagation Test
Section titled “DNS Propagation Test”-- Check if a domain is resolving correctly on different serverslocal function testar_propagacao_dns(dominio, ip_esperado) local servidores = { "8.8.8.8", -- Google "1.1.1.1", -- Cloudflare "9.9.9.9", -- Quad9 "208.67.222.222", -- OpenDNS "64.6.64.6", -- Verisign }
local resultados = {} for _, server in ipairs(servidores) do -- with_coroutine accepts the function and its arguments, returning (result, error) local latencia, err = with_coroutine(dns.ping, server, dominio) local ok = (err == nil)
resultados[server] = { latencia = ok and latencia or nil, acessivel = ok, erro = not ok and err or nil } end
return resultadosend