Skip to content

Instantly share code, notes, and snippets.

@mat813

mat813/convert.pl

Last active Jan 12, 2017
Embed
What would you like to do?
Convert a directory containing zone files into a dlz directory
#!/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