Page 1 of 1

Is there a programmatic way to update DNS TXT record?

Posted: Fri Nov 08, 2019 7:05 pm
by jejaju
For letsencrypt wildcard certs for HTTPS, one step is to update the TXT record. Can I do it programmatically?

Re: Is there a programmatic way to update DNS TXT record?

Posted: Mon Nov 11, 2019 10:55 am
by sysops
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!

Re: Is there a programmatic way to update DNS TXT record?

Posted: Mon Nov 11, 2019 11:39 am
by jejaju
That’s exactly the kind of response I wanted! Thanks so much. I feel that I can work with the CURL example.

Re: Is there a programmatic way to update DNS TXT record?

Posted: Mon Nov 11, 2019 4:28 pm
by sysops
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 :)

Re: Is there a programmatic way to update DNS TXT record?

Posted: Mon Nov 11, 2019 5:50 pm
by jejaju
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

Re: Is there a programmatic way to update DNS TXT record?

Posted: Sun Feb 23, 2020 9:34 pm
by nwhitehorn
It would be extremely nice to have a non-hacky way to do this, including one that doesn't involve disabling 2FA. Is there any hope for an update to the API?