Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Dynamically update DNS entries via API calls
#!/bin/bash
#
# 2016-08-09 20:55
# myrveln@gmail.com
#
DOMAIN="example.com"
PASSWORD="MY_PASSWORD"
# Function to validate an ip address
function ValidateIp()
{
local IP=$1
local RETURN_CODE=1
if [[ $IP =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]; then
OIFS=$IFS
IFS='.'
IP=($IP)
IFS=$OIFS
[[ ${IP[0]} -le 255 && ${IP[1]} -le 255 \
&& ${IP[2]} -le 255 && ${IP[3]} -le 255 ]]
RETURN_CODE=$?
fi
return $RETURN_CODE
}
# Get current external ip, and current DNS record for ${DOMAIN}
CURRENT="$(curl -s -S ifconfig.co)"
DNS="$(dig +short ${DOMAIN} @8.8.8.8)"
# Validate the ip's
ValidateIp ${CURRENT}
RETURN_CURRENT=$?
ValidateIp ${DNS}
RETURN_DNS=$?
# If either of the ip's are invalid, break the script.
# Retrieving the ip's have probably failed.
if [[ ! ${RETURN_CURRENT} -eq 0 ]] || [[ ! ${RETURN_DNS} -eq 0 ]]; then exit 1; fi
# Check whether the ip's differs and perform DNS update via API.
if [[ ${CURRENT} != ${DNS} ]]; then
echo "Running update:"
echo "From \"${DNS}\" to \"${CURRENT}\""
# API call for ClouDNS
#curl -s -S "http://ipv4.cloudns.net/api/dynamicURL/?q=YOUR_KEY"
# API call for Hurricane Electric Free DNS
#curl -ksS "https://dyn.dns.he.net/nic/update" -d "hostname=${DOMAIN}" -d "password=${PASSWORD}" -d "myip=${CURRENT}"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.