Dig Multiple DNS records

Here is a script to dig multiple DNS records for a domain.

#!/bin/bash
ddig(){
    domain=$1
    if [[ "$domain" == *.* ]]; then
        dig1=$(dig +short $domain @8.8.4.4)
        if [[ -z "$dig1" ]]; then
            echo "No A record found for $domain"
        else
            echo -e "\nA:\t$dig1"

            ns_records=$(dig +short NS $domain @8.8.4.4)
            if [[ -z "$ns_records" ]]; then
                echo -e "NS:\tNo NS records found"
            else
                echo -e "NS:\t$ns_records"
            fi

            # Handle multiple IPs by looping over them
            for ip in $dig1; do
                rdns=$(dig +short -x $ip @8.8.4.4)
                if [[ -z "$rdns" ]]; then
                    echo -e "rDNS:\tNo PTR record for $ip"
                else
                    echo -e "rDNS:\t$rdns"
                fi
            done

            mx_records=$(dig +short MX $domain @8.8.4.4)
            if [[ -z "$mx_records" ]]; then
                echo -e "MX:\tNo MX records found"
            else
                echo -e "MX:\t$mx_records"
            fi

            txt_records=$(dig +short TXT $domain @8.8.4.4)
            if [[ -z "$txt_records" ]]; then
                echo -e "TXT:\tNo TXT records found"
            else
                echo -e "TXT:\t$txt_records"
            fi

            dmarc_record=$(dig +short TXT _dmarc.$domain @8.8.4.4)
            if [[ -z "$dmarc_record" ]]; then
                echo -e "DMARC:\tNo DMARC record found\n"
            else
                echo -e "DMARC:\t$dmarc_record\n"
            fi
        fi
    else
        echo "Not a domain"
    fi
}

To run the ddig function and get DNS information for a domain, you can follow these steps:

Save the Script: Save the script as a .sh file, for example, ddig_script.sh.

Make the Script Executable: Open your terminal, navigate to the directory where you saved the script, and make it executable using the following command:

chmod +x ddig_script.sh

Source the Script: To use the ddig function defined in the script, you need to source it in your current shell session:

source ddig_script.sh

Alternatively, you can add the function directly to your .bashrc or .bash_profile file to have it available in all future terminal sessions.

Run the Function: Now, you can use the ddig function by calling it with a domain as an argument. For example, to get the DNS information for example.com, run:

ddig example.com

This will execute the function and display the DNS records for the specified domain. Make sure you’re using a valid domain format (e.g., example.com) when passing the argument.

Leave a Comment