Menü schliessen
Created: September 29th 2023
Last updated: February 16th 2024
Categories: Linux
Author: LEXO

Linux dig Command: Extract All Domain Zone Data with a simple Bash Script

Tags:  bash,  dig,  DNS,  Domain,  Domain Zone Info,  Linux,  script
Donation Section: Background
Monero Badge: QR-Code
Monero Badge: Logo Icon Donate with Monero Badge: Logo Text
82uymVXLkvVbB4c4JpTd1tYm1yj1cKPKR2wqmw3XF8YXKTmY7JrTriP4pVwp2EJYBnCFdXhLq4zfFA6ic7VAWCFX5wfQbCC

Introduction

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.

Why Customize DNS Queries?

Flexibility

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.

Readability

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.

Accuracy

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.

Script Breakdown

Prerequisites

  • Basic understanding of Bash scripting
  • The dig utility installed on your Linux machine

How it Works

Our 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.

The script

#!/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"

How to Use the Script

  1. Save the script to a file (e.g., domaininfo.sh).
  2. Make it executable using chmod +x domaininfo.sh.
  3. Run the script with the domain you are interested in, like so: ./domaininfo.sh example.com.

Script output

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.

Conclusion

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.