How do you programmatically add or remove a DNS record from WHM/cPanel?

Background

The WHM API documentation has lot of information on adding DNS records.

Delete a record is a mess, but we’ll get to that just now.

Add a DNS Zone (TXT)

First, to add a DNS record, it’s best to get into the terminal as root and then you can use this command example:

whmapi1 addzonerecord domain=example.com name=test.example.com class=IN ttl=86400 type=NS txtdata="Test"

As you can see above, there is a specific field txtdata which is designed only for TXT records.

Add a DNS Zone (Name Server)

If, on the other hand, you want to add a name server record, you can follow this recipe:

whmapi1 addzonerecord domain=example.co.za name=example.co.za. class=IN ttl=86400 type=NS nsdname='ns1.example.com'

Not entirely straightforward, because why repeat domain and name, and where does this nsdname come from?

To help you further on your path, follow the official documentation:
https://support.cpanel.net/hc/en-us/articles/1500000323641-How-to-add-a-DNS-record-to-a-domain-using-the-WHM-API-

and
https://documentation.cpanel.net/display/DD/WHM+API+1+Functions+-+addzonerecord

Delete a DNS Zone Record

With regards to deleting a name server record, we refer you to the documentation. A major caveat is you have to read the entire zone file and then delete the record by way of a line number. Not cool nor easy at all.

Here is that documentation:
https://documentation.cpanel.net/display/DD/WHM+API+1+Functions+-+removezonerecord

Reset a Zone

whmapi1 resetzone domain=example.com

Reference: https://documentation.cpanel.net/display/DD/WHM+API+1+Functions+-+resetzone

List all Domains

Official reference: https://api.docs.cpanel.net/openapi/whm/operation/get_domain_info/

Don’t bother, it’s weak and useless.

whmapi1 reference: https://documentation.cpanel.net/display/DD/WHM+API+1+Functions+-+listzones

Better, but if you have multi DNS integration it will list all your zones. So the question remains, how can we only list one server’s domains?

whmapi1 listaccts |awk '/domain:/ {print $2}'

Reset all Zones

How how can you send all this data to the resetzone command?

First generate a list of domains and pipe this out to a file:

whmapi1 listaccts | awk '/domain:/ {print $2}' > domains.txt

Now take the results and pipe it back into a command using the xargs command:

xargs -I % whmapi1 resetzone domain=% < domains.txt

The job is done! You’ve just saved three hours!

Notices about Deprecated Documentation

You may notice that the above API documentation refers to ‘deprecated’:

Basically finding anything WHM/cPanel related for APIs is a convoluted mess. Their API stuff is all over the Internet and most of the routines are overengineered.

With time we’ll update this article to refer to better or newer routines.

For now, gotta get back to helping my clients, life is too short to read reams and reams of overengineered API docs.

Tags

Share this article

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to Top