Coding, How to

How to: Check your DNS entry maps to your Public IP in Bash

I wrote this today as I wanted to ensure that a service waiting for its DNS name to be updated with the correct IP address (its Public IP) before starting.

This little script uses Curl with Akamai’s ‘’ endpoint to get the Public IP and then NSLookup to get the IP returned by the DNS server for the domain. It keeps trying for a while until they match or exits if they don’t match after 250 seconds.

WARNING: In my case it turned out that outbound traffic didn’t route through the same IP as inbound so the script always failed. This may happen to you too if you’re using this in K8s.

WARNING: The AWK logic extracting the IP from the NSLookup is brittle is expects result on line 5. This works on Alpine but may need tweaking, likely are better approaches here.

Run “”

set -e
export DNS_NAME=$1
DNS_IP=$(nslookup $DNS_NAME | awk -F': ' 'NR==5 { print $2 } ')
echo "Checking DNS – IP: $IP DNS_IP: $DNS_IP DNS_NAME: $DNS_NAME"
while [ "$IP" != "$DNS_IP" ] && [ $I -lt $ATTEMPTS ]
DNS_IP=$(nslookup $DNS_NAME | awk -F': ' 'NR==5 { print $2 } ')
IP=$(curl -s
echo "DNS_IP: $DNS_IP"
sleep 10
I=$(( $I + 1 ))
if [ "$IP" != "$DNS_IP" ]
echo "DNS not pointing to our location: FAILED!"
sleep 10
exit 1

view raw

hosted with ❤ by GitHub


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s