Skip to content

Instantly share code, notes, and snippets.

@DvdGiessen
Created September 10, 2018 19:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save DvdGiessen/b4203c69bf0c92f153ea05ed54d804c4 to your computer and use it in GitHub Desktop.
Save DvdGiessen/b4203c69bf0c92f153ea05ed54d804c4 to your computer and use it in GitHub Desktop.
Simple dynamic DNS update script for a domain in DirectAdmin
#!/bin/bash
# Script for automatically updating the DNS records using the DirectAdmin API.
# Requires you to have curl, dig and json installed.
#
# By Daniël van de Giessen, 2018-09-10
# https://www.dvdgiessen.nl
# Configuration
DOMAINNAME="example.com"
DA_URL="https://directadmin.example.com:2222"
DA_USERNAME="example"
DA_PASSWORD="create_a_login_key_in_direct_admin_and_give_it_access_to_only_CMD_API_DNS_CONTROL"
# Get the currently configured IP address from the authoritive nameserver
CONFIGURED_IP="$(dig +short "${DOMAINNAME}" A @$(dig +short "${DOMAINNAME}" NS | head -n1))"
# Get current external IP address of this machine
CURRENT_IP="$(dig +short myip.opendns.com @resolver1.opendns.com)"
# Check if the IP needs to be updated
if [[ "${CONFIGURED_IP}" != "${CURRENT_IP}" ]] ; then
# Change the IP through the DA API
# Note: Used --insecure because our DA instance doesn't return the complete intermediate certificate.
# If yours does, it is probably a good idea to remove the --insecure flag
# Browsers deal with this fine since they have the intermediate in their store. curl does not.
RESULT="$(curl -sS --insecure -u "${DA_USERNAME}:${DA_PASSWORD}" "${DA_URL}/CMD_API_DNS_CONTROL?domain=${DOMAINNAME}&action=edit&arecs0=name%3D${DOMAINNAME}.%26value%3D${CONFIGURED_IP}&type=A&name=${DOMAINNAME}.&value=${CURRENT_IP}&json=yes")"
if [[ "$(echo "${RESULT}" | json success)" == "record added" ]] ; then
echo "Updated IP address for ${DOMAINNAME} on DirectAdmin from ${CONFIGURED_IP} to ${CURRENT_IP}! :)"
else
echo "Failed to update IP address for ${DOMAINNAME} on DirectAdmin from ${CONFIGURED_IP} to ${CURRENT_IP}! :(" >&2
echo "${RESULT}" >&2
fi
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment