Adding DNS-records via API through bash/curl

porkpiehat

New member
Joined
Jan 16, 2019
Messages
2
Hi all,

I'm trying to automatically update DNS records from two external servers. They are subdomains of my normal domain (reseller hosting). There are PHP scripts to do this, but the servers do not have PHP installed - and I'm not planning on installing it. So bash it is.

In the Help there is an article on how to use the API through curl/bash. From the API help I found the right commands to do so. Yet, it does not work yet...

Getting a dump of the records works fine. When trying to add or delete a record, firstly a '0' is returned (usually a good sign), followed by a dump of the unchanged records. No error message whatsoever.
When I paste the command in my browser (when logged in to DirectAdmin), adding and deleting the record works just fine.

Any thoughts on this?

The bash script:
Code:
#!/bin/bash

# User and server details
user="username"
password="password"
server="https://webserver.hosting.com:portnumber"
domain="mydomain.com"
subdomain="test"

# Retrieve DNS records
#curl --request POST --user $user:$password --data 'package=unlimited' $server/CMD_API_DNS_CONTROL?domain=$domain
#echo $?

# Delete
curl --request POST --user $user:$password --data 'package=unlimited' --max-time 15\
		$server/CMD_API_DNS_CONTROL?domain=$domain&action=select&arecs0=name=$subdomain
echo $?

# Add
curl --request POST --user $user:$password --data 'package=unlimited' --max-time 15\
		$server/CMD_API_DNS_CONTROL?domain=$domain&action=add&type=A&name=$subdomain&value=8.8.8.8
echo $?
 

zEitEr

Super Moderator
Joined
Apr 11, 2005
Messages
14,128
Location
GMT +7.00
Hello,

Why do you send data "package=unlimited" ? It's not needed here, try:
Code:
[/FONT]# Add
curl --request GET --user $user:$password $server/CMD_API_DNS_CONTROL?domain=$domain&action=add&type=A&name=$subdomain&value=8.8.8.8
echo $?
For deleting you should URL-encode name=www&value=1.2.3.4 so it will be something like:

$server/CMD_API_DNS_CONTROL?domain=$domain&action=select&arecs0=name%3Dwww%26value%3D1.2.3.4
 

porkpiehat

New member
Joined
Jan 16, 2019
Messages
2
Thanks for the reply. The "package=unlimited" was added by the curl command builder from the DirectAdmin help.
The URLs and commands I had were correct since they worked in the browser. But, the URL had variables in there that needed to be parsed too. Adding quotes around it was the trick..

With cleaning up all redundant parameters, this now works:

Code:
# Updating requires delete & add.
# delete
curl -s --max-time 15 --user $user:$password "${server}/CMD_API_DNS_CONTROL?domain=${domain}&action=select&arecs0=name%3D${subdomain}
# add
curl -s --max-time 15 --user $user:$password "${server}/CMD_API_DNS_CONTROL?domain=${domain}&action=add&type=A&name=${subdomain}&value=${newIP}
 
Top