Skip to content

Instantly share code, notes, and snippets.

@colinmollenhour
Created July 10, 2014 16:26
Show Gist options
  • Save colinmollenhour/30398f3b79c3bf47e585 to your computer and use it in GitHub Desktop.
Save colinmollenhour/30398f3b79c3bf47e585 to your computer and use it in GitHub Desktop.
#!/bin/bash
#
# Store/update the DNS entry locally. Requires unbound install as a local DNS resolver
# and a cron job that runs this script.
#
# Copyright: Colin Mollenhour 2014
#
# Recommended DNS servers for querying:
# OpenDNS: 208.67.222.222 and 208.67.220.220
# Google: 8.8.8.8 and 8.8.4.4
name=$1
server=$2
if [ "$name" = "" ]; then
echo "Usage: $0 <domain_name> [dns_server]"
exit 1
fi
if [ "$server" = "" ]; then
server=208.67.222.222
fi
cached=$(unbound-control list_local_data | grep $name | awk '{ print $5; }')
current=$(dig @$server $name A +short | tail -n 1)
if [ "$current" != "" ] && [[ $current =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]; then
if [ "$cached" != "$current" ]; then
unbound-control local_data_remove $name > /dev/null
unbound-control local_data $name A $current > /dev/null
fi
else
echo "Failed to resolve $name using $server ($current)."
exit 1
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment