Update zone file with a script

I’d like to update a zone file from a CLI script - change the IP address for a single host, and update the “last-updated” SOA serial entry.

What’s the best way to do this? Are there any existing tools that help automated zone file editing without breaking them and that keep existing formatting?

(This would be a nice application for the nsupdate cli tool, but NSD does not support changing zone files remotely. dns-lexicon does not support zone files yet.)

It’s very difficult to write tools to edit zone files, because the file format is so lenient.

If you just want to update a specific record in your zone file, then it’s easy to do that with the usual tools like sed/awk/perl, because you know how your zone file is structured. This is also the best way to keep your zone file ordering and comments.

Another way would be to write a script in python, using the “dnspython” module. You would load the zone into memory, update the record(s) you want to (including serial number), and then write out the zone back to disk, but you would lose all the comments and ordering.

Regards,

Anand

I ended up writing zone part files from my application, which are loaded from the actual zone files with $INCLUDE:

$INCLUDE /etc/nsd/zones/dyndns/home.cweiske.de-v4.zonepart
$INCLUDE /etc/nsd/zones/dyndns/home.cweiske.de-v6.zonepart

The .zonepart files are regenerated from scratch each time; they only contain two entries.

The serial number of the actual zone file needs to be updated, though - I use zsu for that: https://metacpan.org/release/AZS/zsu-1.20


I also found that dnslexicon is able to update zone files: Configuration reference — DNS-Lexicon 3.25.1 documentation