Troubleshooting Intermittent DNS Resolution Issues


DNS is something so fundamental to most of our systems functioning that it’s often overlooked in initial troubleshooting, it’s also incredibly hard to troubleshoot if it’s only intermittently failing.

The below script helps me in doing this kind of troubleshooting by effectively bombarding a DNS server with requests and reporting whenever the response changes.

#!/bin/bash

dns_name=$1
dns_server=$2
detail_log_file=$3

if [ -z "$detail_log_file" ]; then
    detail_log_file="/dev/stdout"
fi


if [ -z "$dns_name" ] || [ -z "$dns_server" ] || [ -z "$detail_log_file" ]; then
    echo "Usage: $0 <dns_name> <dns_server> [detail_log_file=/dev/stdout]"
    exit 1
fi

previous_output=""
while true; do
    # Run the dig command
    output=$(dig +short $dns_name "@$dns_server")

    # Check the exit status of the dig command
    if [ "$output" != "$previous_output" ]; then
        timestamp=$(date +"%Y-%m-%d %H:%M:%S")
        echo "[$timestamp] dig response changed"

        echo "------" >> $detail_log_file
        echo "# [$timestamp] response changed" >> $detail_log_file
        echo "# previous response" >> $detail_log_file
        echo "$previous_output" >> $detail_log_file
        echo "# new response" >> $detail_log_file
        echo "$output" >> $detail_log_file

        previous_output="$output"
    fi
done

See also