0

We have a standard high-availability setup at a co-location facility that we host client programs on. For some programs, we host the DNS, but I find that setting up multiple zones manually can be tedious and prone to error. Is there an easy way to setup a standard zone file template and some how run sed, awk, or some other program to process a set of variables from another file and merge them with the template to create a zone file?

I thought about using m4, but I'm not well versed and it only seems ideal for merging one template at a time.

Ideally, I'd like to take a standard zone file template and merge it with a list like so:

domain1=ip1
domain2=ip2
domain3=ip1
domain4=ip1
etc...

Is there a way to script this?

Chris
  • 869
  • 1
  • 7
  • 13

1 Answers1

3

I think m4 would be overkill. My first instinct would be bash + sed, but if you have any familiarity with a higher-level language that might be easier. Off the top of my head, I would imagine something like this:

Data file:

example.com:192.168.10.1
fake.com:192.168.10.2
foo.com:192.168.10.3
bar.com:192.168.10.4

Script:

#!/bin/bash

DATAFILE=datafile
TEMPLATE=template.txt

for data in $(cat $DATAFILE)
do
    dom=${data%:*}
    ip=${data#*:}
    sed "{ s/DOMAIN/$dom/;
           s/IPADDR/$ip/;
         }" $TEMPLATE > $dom.zone
done

And your template zone file would have "DOMAIN" or "IPADDR" where appropriate.

This is completely untested and should be refined before use.

Insyte
  • 9,394
  • 3
  • 28
  • 45