Is there a programmatic way to update DNS TXT record?

Web hosting discussion, programming, and shared and dedicated servers.
5 posts Page 1 of 1
by jejaju » Fri Nov 08, 2019 7:05 pm
For letsencrypt wildcard certs for HTTPS, one step is to update the TXT record. Can I do it programmatically?
by sysops » Mon Nov 11, 2019 10:55 am
Sonic doesn't seem to have any kind of API to modify DNS records other than their DynDNS API which only lets you update A or AAAA records, but it looks pretty trivial to write a small script to log in to your Member Tools, and submit a form post that would update one of your records.

Each record appears to have an ID associated with it, so once logged in, you could make a POST request similar to the following with curl that would update a TXT record.

Code: Select all

# log in
curl -Lv -c cookies.txt -d login=login -d user=your_username -d 'pw=your password' https://members.sonic.net/

# update DNS
curl -Lv -b cookies.txt -c cookies.txt -d a=UpdateDNS -d dom_id=yourdomain.net -d update_id=your_record_id -d recordName= -d recordTTL=900 -d 'recordContent="your txt record content"' https://members.sonic.net/websites/nameservers/host_records/


2 Factor Auth will need to be off on your account. I tested this with one of my DNS records and it successfully updated it. Translate into any language, but that 2-liner curl script will do it. Be advised, no error checking here as to whether login fails or if the update fails.

Hope that helps!
Proud Sonic customer since 1999. Ask me about internet privacy, VPN, anonymity and security.
by jejaju » Mon Nov 11, 2019 11:39 am
That’s exactly the kind of response I wanted! Thanks so much. I feel that I can work with the CURL example.
by sysops » Mon Nov 11, 2019 4:28 pm
You're welcome (:

If you keep it in bash with simple commands, I'd probably throw lazy error checking in by piping the HTML responses to a file, grepping for your expected output, and then checking grep's return value to see if you matched or not.

Code: Select all

# log in
curl -Lv -c cookies.txt -d login=login -d user=your_username -d 'pw=your password' https://members.sonic.net/ > /tmp/sonic.txt

grep 'You are logged in as' /tmp/sonic.txt
if [ $? != 0 ] ; then
    echo "Failed to log in!"
    exit 1
fi

# add similar check for the record update here :)
Proud Sonic customer since 1999. Ask me about internet privacy, VPN, anonymity and security.
by jejaju » Mon Nov 11, 2019 5:50 pm
Thanks! I’ve already written my script…

Code: Select all

#!/bin/zsh
#
#   Update the _acme-challenge TXT record
#
content=$1
[b]user=XXXX
pw=YYY
dom_id=ZZZ.com
update_id=nnnnnnnnnn
[/b]ttl=100
# log in
welcomed=`curl -L -c cookies.txt -d login=login -d user=$user -d pw=$pw https://members.sonic.net/ 2>/dev/null|grep "Welcome to the Sonic Member Tools"`
#
if [[ -z "${welcomed// }" ]]
then
    echo "We have a login problem"
else
    updated=`curl -L -b cookies.txt -c cookies.txt -d a=UpdateDNS -d dom_id=$dom_id -d update_id=$update_id -d recordName=_acme-challenge -d recordTTL=$ttl -d recordContent=\"$content\" https://members.sonic.net/websites/nameservers/host_records/ 2>/dev/null|grep ${content}`
    if [[ -z "${updated// }" ]]
    then
        echo "We have a update problem"
    else
        echo "updated _acme-challenge!"
    fi
fi
rm cookies.txt
5 posts Page 1 of 1

Who is online

In total there are 5 users online :: 0 registered, 0 hidden and 5 guests (based on users active over the past 5 minutes)
Most users ever online was 422 on Sat May 26, 2012 5:28 am

Users browsing this forum: No registered users and 5 guests