edit DNS record

vespino

Verified User
Joined
Nov 21, 2017
Messages
12
I'm just today starting with the DirectAdmin API and my goal is to edit DNS records, specifically the one pointing to my home location. Although the IP almost never changes, if it does this could give a headache when I'm not home. That is why I'm hoping to change a certain record to the new IP in the case it does change. I was able to do this by first deleting the existing record and then adding it again. Although this causes a bit of an issue since I have to target a certain record based on the order they're in, but this works. Looking further into the API I have found I can first get a list of records and simply loop all of them and edit those where my old IP is present, but my guess is this could be done more efficient (although in my DA interface it isn't possible to edit records come to think of it).

2 questions I have:
  • Is it possible to edit DNS records, so not delete/re-add it?
  • If not: how do I delete a TXT record? I know I can target the first A record by using arecs0, but I can't find this value for TXT records.
 
Please explain because I’m only familiar with dynamic dns using somename.mine.nu or something like this and not using my own domain. Btw, I have this working the way I had hoped now (first deleting and re-adding the record that it), so I have no direct need for it, but who knows, it might be useful.
 
I think he ment that, but dyndns.org or mine.nu or something is not your own domain, that works differently.
But maybe you can set that up with api's.

Have a look at this thread, a lot of usefull information.

Also this could be interesting:

Be aware, I did not test any of this.
 
I made a little shell script that does this. It may or may not be useful for you:

Bash:
#!/bin/bash

# Simple script to automatically update a DNS A record to point to your current
# IP address using the DirectAdmin API. Requires curl, dig and jq.
#
# There must already exist an A record for DNS_HOST, or this script will abort
# and complain about an invalid IP address.
#
# Based on <https://gist.github.com/DvdGiessen/b4203c69bf0c92f153ea05ed54d804c4>

################################################################################
# Configuration start

# The domain for the account in DirectAdmin
DOMAINNAME="some.domain.here"

# The hostname for your A record
DNS_HOST="myhost.some.domain.here"

# The URL to the DirectAdmin server we log in to
DA_URL="https://your.da.server:2222"

# The username and password for the account owning DOMAINNAME. Note that the
# password here should ideally be a Login Key that only has access to
# CMD_API_DNS_CONTROL and nothing else.
DA_USERNAME="username"
DA_PASSWORD="secret"

# Configuration end
################################################################################

# Validate an IP addres
#
# Returns 0 if IP is valid
# Returns 1 if IP is invalid
#
# Usage:
#   validateIP IPADDRESS
#   if [ ${?} -eq 0 ]; then
#       echo good
#   else
#       echo bad
#   fi
# OR
#   if validateIP IPADDRESS; then
#       echo good
#   else
#       echo bad
#   fi
function validateIP() {
    local ip=${1}
    local validresult=1

    # First check against a simple regex
    if [[ ${ip} =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]; then
        # Then check each number
        OIFS=${IFS}
        IFS='.'
        ip=(${ip})
        IFS=${OIFS}
        [[     ${ip[0]} -le 255 \
            && ${ip[1]} -le 255 \
            && ${ip[2]} -le 255 \
            && ${ip[3]} -le 255 ]]
        validresult=${?}
    fi
    return ${validresult}
}

# Get the current IP address from DNS, and validate it.
CONFIGURED_IP="$(dig +short "${DNS_HOST}" A @$(dig +short "${DOMAINNAME}" NS | head -n1))"
if ! validateIP "${CONFIGURED_IP}"; then
    echo "Invalid configured IP: ${CONFIGURED_IP}. Aborting." >&2
    exit
fi

# Get current external IP address of this machine, and validate it.
CURRENT_IP="$(curl -s https://api.myip.com/ | jq -r .ip)"
if ! validateIP "${CURRENT_IP}"; then
    echo "Invalid active IP: ${CURRENT_IP}. Aborting." >&2
    exit
fi

# Check if the DNS A record needs to be updated
if [ "${CONFIGURED_IP}" != "${CURRENT_IP}" ]; then
    # Update the DNS A record and report.
    # TODO:
    # * Change this to a POST?
    RESULT="$(curl -sS -u "${DA_USERNAME}:${DA_PASSWORD}" "${DA_URL}/CMD_API_DNS_CONTROL?action=edit&domain=${DOMAINNAME}&arecs0=name%3D${DNS_HOST}.%26value%3D${CONFIGURED_IP}&type=A&name=${DNS_HOST}.&value=${CURRENT_IP}&json=yes")"
    if [ "$(echo "${RESULT}" | jq -r .success)" == "Record Edited" ]; then
        echo "$(date +"%F %T") DNS record updated: ${DNS_HOST} -> ${CURRENT_IP}"
    else
        echo "$(date +"%F %T") DNS record update failed" >&2
        echo "${RESULT}" >&2
    fi
fi
 
There are services
I do believe you as these services are known to me.
But they won't update your local DNS settings, you have to run your DNS service externally like for example EasyDNS or probably also Cloudflare.

As far as I understood @vespino want's his local DA DNS servers at home to be updated, if I'm not mistaken.
 
@kristian thanks, I probably only need this line, but thanks!

Code:
RESULT="$(curl -sS -u "${DA_USERNAME}:${DA_PASSWORD}" "${DA_URL}/CMD_API_DNS_CONTROL?action=edit&domain=${DOMAINNAME}&arecs0=name%3D${DNS_HOST}.%26value%3D${CONFIGURED_IP}&type=A&name=${DNS_HOST}.&value=${CURRENT_IP}&json=yes")"

@Richard G @BillyS what I'm trying to do is update the DNS on a hosted DA whenever the IP address of my home connection changes so that when I'm away from home and this happens, I don't have to handle this remotely. If this is done automatically, everything keeps running as it should without having to intervene.
 
Yes but this is a DNS on a hosted DA at your home, correct?
Because if the DNS is not hosted at your home, then @BillyS solution would be the correct solution.
The DA or DNS aren’t at my home. I have changed my delete/re-add script to edit the way you suggested and this will work for me. No need to include yet another service.
 
Oke then that is more clear now.
If another solution is needed in the future (or for anybody reading this looking for a similar solution), then check @BillyS solution and for example easydns and kindlike options.
 
Back
Top