Last active
January 12, 2017 18:37
-
-
Save mat813/aa00cb7b781975b9f7e947dbc0450dc8 to your computer and use it in GitHub Desktop.
Convert a directory containing zone files into a dlz directory
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env perl | |
# | |
use 5.008; | |
use utf8; | |
use strict; | |
use warnings; | |
use File::Spec; | |
use File::Path qw(make_path); | |
use DNS::ZoneParse; | |
use Data::Dumper; | |
use Readonly; | |
use Carp; | |
my $directory = $ARGV[0]; | |
Readonly::Scalar my $SEPARATOR => q(#); | |
my $dh; | |
opendir $dh, $directory; | |
sub touch { | |
my $out = File::Spec->catfile(@_); | |
return if -e $out; | |
my $f; | |
open $f, '>', $out or carp "$out -> $!"; | |
close $f or carp "$out -> $!"; | |
return; | |
} | |
sub mkdata { | |
return join($SEPARATOR, @_) . $SEPARATOR; | |
} | |
while (my $file = readdir $dh) { | |
my $fullpath = File::Spec->catfile($directory, $file); | |
next if -d $fullpath; | |
my $basepath = File::Spec->catfile(reverse split /[.]/ms, $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'); | |
my $zh; | |
open $zh, '<', $fullpath; | |
my $origin = $file . q(.); | |
while (my $line = <$zh>) { | |
chomp $line; | |
my ($rr, $ttl, $in, $type, $rrdata) = split /\s+/oms, $line, 5; | |
my $rrname; | |
if ($origin eq $rr) { | |
$rrname = q(@); | |
} else { | |
$rrname = $rr; | |
$rrname =~ s/[*]/-/oms; | |
$rrname =~ s/[.]$origin//ms; | |
} | |
my $rrpath = File::Spec->catfile($dnspath, reverse split /[.]/ms, $rrname); | |
make_path($rrpath); | |
$rrdata =~ s/\//%/ogms; | |
touch($rrpath, mkdata($type, $ttl, $rrdata)); | |
} | |
close $zh or carp $!; | |
} | |
closedir $dh; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment