Could we help you? Please click the banners. We are young and desperately need the money
DNS, or Domain Name System, is the phonebook of the internet. Whenever you type a URL into your browser, DNS translates it into an IP address that computers understand. As Linux users, we often find ourselves in situations where we need to dig deep into DNS records for debugging, monitoring, or configuration tasks. Most people use the dig
command-line utility for this purpose, but what if we could customize it to our specific needs?
In this blog post, we'll introduce a Bash script that utilizes dig
to query multiple types of DNS records and formats the output for easier interpretation. This tutorial is specifically aimed at Linux users interested in enhancing their DNS querying capabilities.
With a customizable script, you can decide what types of DNS records to query and in what order they should be displayed. Whether you're interested in "A", "AAAA", "CNAME", "MX", "TXT", "NS", or "SOA" records, you can set the order according to your needs.
dig
itself can sometimes produce output that's hard to sift through, especially if you're interested in multiple types of records at once. A custom script can format this output in a more human-readable way.
When debugging DNS issues, it's crucial to get the most accurate information, including the Time to Live (TTL). This script fetches authoritative DNS information, making it a reliable tool for DNS debugging.
dig
utility installed on your Linux machineOur script uses a Bash array to store different types of DNS records we are interested in. Using a for
loop, the script iterates through this array, querying each record type for a given domain. The awk
utility then formats the output.
The script also identifies the authoritative name server for the given domain to fetch the most accurate TTL and record information.
#!/bin/bash
domain=$1
auth_ns=$(dig NS $domain +short | head -1)
# Check if the dig command failed or if the output is empty
if [ -z "$auth_ns" ]; then
echo "Error: No NS records for [ $domain ] could be determined."
exit 1
fi
echo -e "\nNameserver queried: $auth_ns \n"
dig @${auth_ns} ${domain} any +noall +answer +timeout=5 +tries=3 +tcp +noquestion +noqr +nomultiline +nokeepalive +noidentify +nofail +noexpire +noadditional +noauthority +nocomments |sort -t$'\t' -k4
echo -e "\n"
domaininfo.sh
).chmod +x domaininfo.sh
../domaininfo.sh example.com
.This is the output the script will generate:
lexo.ch. 60 IN A 185.104.85.133
lexo.ch. 60 IN MX 5 mail.lexo.ch.
lexo.ch. 60 IN NS ns1.lexo.ch.
lexo.ch. 60 IN NS ns2.lexo.ch.
Custom Bash scripts offer Linux users the ability to tailor commands to their specific needs. In the realm of DNS, our script serves as an excellent example of how we can fetch and display multiple types of DNS records in a format we desire, all while ensuring the accuracy of the data retrieved.
Whether you're a system administrator or a curious Linux user, this customizable script can be a handy addition to your toolbox.