Skip to content

Instantly share code, notes, and snippets.

@mat813
Last active January 12, 2017 18:37
Show Gist options
  • Select an option

  • Save mat813/aa00cb7b781975b9f7e947dbc0450dc8 to your computer and use it in GitHub Desktop.

Select an option

Save mat813/aa00cb7b781975b9f7e947dbc0450dc8 to your computer and use it in GitHub Desktop.
Convert a directory containing zone files into a dlz directory
#!/usr/bin/env perl
#
use utf8;
use strict;
use warnings;
use File::Spec;
use File::Path qw(make_path);
use DNS::ZoneParse;
use Data::Dumper;
my $directory = $ARGV[0];
use constant SEPARATOR => '#';
opendir((my $dh), $directory);
sub touch {
my $out = File::Spec->catfile(@_);
return if -f $out;
open(my $f, '>', $out) or warn "$out -> $!";
close $f;
}
sub mkdata {
join(SEPARATOR, @_) . SEPARATOR;
}
while (my $file = readdir($dh)) {
my $fullpath = File::Spec->catfile($directory, $file);
next unless -f $fullpath;
my $basepath = File::Spec->catfile(reverse split(/\./, $file));
my $xfrpath = File::Spec->catfile($basepath, 'xfr_d');
my $dnspath = File::Spec->catfile($basepath, 'dns_d');
make_path($basepath, $xfrpath, $dnspath);
touch($xfrpath, '127.0.0.1');
open((my $zh), '<', $fullpath);
my $origin = $file . '.';
while (my $line = <$zh>) {
chomp($line);
my ($rr, $ttl, $in, $type, $data) = split(/\s+/o, $line, 5);
my $rrname;
if ($origin eq $rr) {
$rrname = '@';
} else {
$rrname = $rr;
$rrname =~ s/\*/-/o;
$rrname =~ s/\.$origin//;
}
my $rrpath = File::Spec->catfile($dnspath, reverse split(/\./, $rrname));
make_path($rrpath);
$data =~ s/\//%/og;
touch($rrpath, mkdata($type, $ttl, $data));
}
}
closedir($dh);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment