loading...

Creating DNS records programmatically

funkysi1701 profile image Simon Foster Originally published at funkysi1701.com on ・2 min read

DNS is the backbone of the internet. It converts domain names into IP addresses. But how can we do clever things with it?

Making DNS changes has always been a pain. You have to remember what company you registered your domain name with, then locate the login details for their website, login and fill in some web form and wait for the changes to replicate across the internet.

There are various websites that assign you a custom subdomain name like name.domain.com, obviously they must do this programmatically, they don’t have a guy following the above process.

Lets see how we could do this.

My first thought was to make use of the DNSimple API. https://developer.dnsimple.com/v2/ The API allows all sort of DNS changes to be made, however this is a paid service so let’s look at other options.

Azure now has a DNS section. You can go to the portal, add a DNS zone, and then add as many A, CNAME and MX records as you need. This is of course no better than doing it manually.

Azure offers a SDK and an example Visual Studio project. Lets look at how it works.

First you need to setup some credentials to use your Azure subscription. The easiest way to do this is with powershell.

.gist table { margin-bottom: 0; }

This will create a AD Service Principal which has access to your Azure subscription.

You need to get the following IDs from the Azure portal.

subscriptionId for your subscription

tenantId or the Azure AD Directory ID

applicationId of the service principal created above

password you entered into the powershell script above

.gist table { margin-bottom: 0; }

The above code connects to Azure and creates an A record for test.funkysi1701.com that points at the ip address 1.2.3.4.

Other DNS records can be created in a similar way.

The above example creates a new RecordSet but you can use the following to delete or get existing records.

.gist table { margin-bottom: 0; }

Now I have found out that I can write a console app to edit my DNS records I need to change the nameservers for all my domains so I can take advantage of this new feature.

The post Creating DNS records programmatically appeared first on Funky Si's Tech Talk.

Posted on by:

funkysi1701 profile

Simon Foster

@funkysi1701

Software Developer based in the North of England, creator of Pwned Pass a mobile app based on the haveibeenpwned data. More than just another developer.

Discussion

pic
Editor guide