Automagically generate reverse from forward zones

Given a (forward) zone file, here is a quick way to automagically generate all the reverse zone files you need:

# does not generate PTR records for $GENERATE lines
# does not generate PTR records for CNAME and AAAA RRs
# generates PTR records for A RRs
%revzones = ();
$domain = $ARGV[0] || "";
$filter = $ARGV[1] || '\S+';

open (F, "<$domain");
while (<F>) {
	next if ($_ =~ /^[;\#\$]/);
	if ($_ =~ /(\S+)[\s]+(\d+[\s]+)?IN[\s]+A[\s]($filter)/) {
		$host = $1;
		$ip = $3;
		($a, $b, $c, $d) = split (/\./, $ip);
		$revzone = "$c.$b.$";
		$ptr = "$d\tIN\tPTR\t$host.$domain.\n";
		$revzones{$revzone} .= $ptr;
close (F);

open (NC, ">named.conf.rev");
foreach $revzone ( sort keys %revzones ) {
	open (R, ">$revzone");
	print R "\$INCLUDE \"\"\n";
	print R "$revzones{$revzone}";
	close (R);
	print NC "zone \"$revzone\" { type master; file \"rev/$revzone\"; };\n";
close (NC);

Careful as it will overwrite any existing reverse zone files you have in your current working directory.

PS. I do have one zone that generates 263 reverse zone files!


3 Replies to “Automagically generate reverse from forward zones”

    1. Around 2000, @Internet Hell-as, I wrote something similar for the horde of «register 562 domains for this one customer + pollute our DNS with them» tasks I’ve been tortured with. Automagically managed forward/reverse zones from templates, *and* it directly submitted the registration data from the customer database to’s domain registration form ;)

      I feel somewhat strange that now, 10 years later, we mostly ασχολουμαστε με τις ιδιες μαλακιες…


Εισάγετε τα παρακάτω στοιχεία ή επιλέξτε ένα εικονίδιο για να συνδεθείτε:


Σχολιάζετε χρησιμοποιώντας τον λογαριασμό Αποσύνδεση /  Αλλαγή )

Φωτογραφία Twitter

Σχολιάζετε χρησιμοποιώντας τον λογαριασμό Twitter. Αποσύνδεση /  Αλλαγή )

Φωτογραφία Facebook

Σχολιάζετε χρησιμοποιώντας τον λογαριασμό Facebook. Αποσύνδεση /  Αλλαγή )

Σύνδεση με %s

Αρέσει σε %d bloggers: