Automating DNS Propagation Checks with dig on Linux

I do a lot of hosting migrations on Codeable to help WordPress site owners make their site faster. For anybody who has migrated hosts before you know the major bottleneck is DNS and its slow propagation.

I usually check DNS records with DNS Checker and it works very well but it requires manual labor. I prefer to automate wherever possible so I decided to make this little script to send me a Pushbullet notification when the name servers have updated. It can be adapted to send an email instead and for other DNS record types.

Automating DNS Propagation Checks with dig on Linux

On Debian and Ubuntu systems you need the dnsutils package

sudo apt-get update
sudo apt-get install dnsutils -y

On CentOS it is called bind-utils

yum install bind-utils

Now we are ready to use the dig command.

Quick DNS Propagation Checks

The main records I usually have to check for a host migration are

  • NS records – name servers
  • A records
  • CNAME records
  • TXT records

Name Servers

To get your name servers

dig wp-bullet.com NS +short

Notice the . at the end of the domain name. This is actually always present but hidden in browsers.

The . stands for root (start here) since DNS is processed from right to left.

dawn.ns.cloudflare.com.
burt.ns.cloudflare.com.

A Records

To get your domain’s A records

dig wp-bullet.com A +short

Output, notice there are 2 because I use Cloudflare

104.31.88.5
104.31.89.5

CNAME Records

Checking for CNAMEs remember to add the prefix to your domain, here email.mg

dig email.mg.wp-bullet.com CNAME +short

Output, notice again the . at the end

mailgun.org.

TXT Records

To get TXT records – useful if you are adding email verification for Mailgun, Sendgrid or Gsuite

dig mg.wp-bullet.com TXT +short

Output

"v=spf1 include:mailgun.org ~all"

Checking DKIM TXT records for signing email so it doesn’t go to spam folders

dig smtp._domainkey.mg.wp-bullet.com TXT +short

Those are the most common record types I check, now let’s automate it.

Automation Script Example

Here is how I am checking if name servers have been updated, it can be adjusted for other DNS record types.

Create a scripts folder

mkdir ~/scripts

Create a new empty script

nano ~/scripts/dns-ns-check.sh

Paste the script below and adjust the follow variables

  • DOMAIN is your domain name
  • PUSHBULLET is your Pushbullet API Key
  • NSUPDATED[0] is the primary name server
  • NSUPDATED[1] is the secondary name server
#!/usr/bin/env bash
# Purpose: DNS Propagation checker
# Source: https://guides.wp-bullet.com

# make sure DNS utilities are installed
if hash dig 2>/dev/null; then
    echo "DNS utilities not installed"
    exit
fi

# domain to check
DOMAIN=wp-bullet.com

# pushbullet API key
PUSHBULLET=APIKEY

# array of desired nameservers in order, remember . at the end
NSUPDATED[0]=dawn.ns.cloudflare.com.
NSUPDATED[1]=burt.ns.cloudflare.com.

# make the DNS NS check
WPBULLETNS=($(dig $DOMAIN NS +short))

# variables for comparison checks
WPBULLETNSVAR=${WPBULLETNS[@]}
NSUPDATEDVAR=${NSUPDATED[@]}

# check if nameservers are equal and send pushbullet notification
if [ "$WPBULLETNSVAR" == "$NSUPDATEDVAR" ]; then
    curl --header "Access-Token: $PUSHBULLET" \
     --header 'Content-Type: application/json' \
     --data-binary '{"body":"DNS NS Updated","title":"'"$DOMAIN"'","type":"note"}' \
     --request POST \
     https://api.pushbullet.com/v2/pushes
fi

Ctrl+X, Y and Enter to Save and Exit

Make the script executable

sudo chmod +x ~/scripts/dns-ns-check.sh

We are going to schedule the script in a cronjob

crontab -e

This will run the script every 5 minutes, change 5 to another value if you would like to use a different interval.

*/5 * * * * /bin/bash /home/wpbullet/scripts/dns-ns-check.sh

You should see a pushbullet notification when the name servers have updated 🙂

Sources

Check DNS Records with dig
Install dig on CentOS 6
DNS Propagation Checker
Check 2 Arrays are Equal bash
Pushbullet API Docs