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.

Revisions

  1. mat813 revised this gist Jan 12, 2017. 1 changed file with 29 additions and 20 deletions.
    49 changes: 29 additions & 20 deletions convert.pl
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,7 @@
    #!/usr/bin/env perl
    #

    use 5.008;
    use utf8;
    use strict;
    use warnings;
    @@ -9,60 +10,68 @@
    use File::Path qw(make_path);
    use DNS::ZoneParse;
    use Data::Dumper;
    use Readonly;
    use Carp;

    my $directory = $ARGV[0];

    use constant SEPARATOR => '#';
    Readonly::Scalar my $SEPARATOR => q(#);

    opendir((my $dh), $directory);
    my $dh;
    opendir $dh, $directory;

    sub touch {
    my $out = File::Spec->catfile(@_);
    return if -f $out;
    open(my $f, '>', $out) or warn "$out -> $!";
    close $f;
    return if -e $out;
    my $f;
    open $f, '>', $out or carp "$out -> $!";
    close $f or carp "$out -> $!";
    return;
    }

    sub mkdata {
    join(SEPARATOR, @_) . SEPARATOR;
    return join($SEPARATOR, @_) . $SEPARATOR;
    }

    while (my $file = readdir($dh)) {
    while (my $file = readdir $dh) {
    my $fullpath = File::Spec->catfile($directory, $file);
    next unless -f $fullpath;
    next if -d $fullpath;

    my $basepath = File::Spec->catfile(reverse split /[.]/ms, $file);

    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 $zh;
    open $zh, '<', $fullpath;

    my $origin = $file . '.';
    my $origin = $file . q(.);

    while (my $line = <$zh>) {
    chomp($line);
    chomp $line;

    my ($rr, $ttl, $in, $type, $data) = split(/\s+/o, $line, 5);
    my ($rr, $ttl, $in, $type, $rrdata) = split /\s+/oms, $line, 5;

    my $rrname;
    if ($origin eq $rr) {
    $rrname = '@';
    $rrname = q(@);
    } else {
    $rrname = $rr;
    $rrname =~ s/\*/-/o;
    $rrname =~ s/\.$origin//;
    $rrname =~ s/[*]/-/oms;
    $rrname =~ s/[.]$origin//ms;
    }

    my $rrpath = File::Spec->catfile($dnspath, reverse split(/\./, $rrname));
    my $rrpath = File::Spec->catfile($dnspath, reverse split /[.]/ms, $rrname);
    make_path($rrpath);

    $data =~ s/\//%/og;
    touch($rrpath, mkdata($type, $ttl, $data));
    $rrdata =~ s/\//%/ogms;
    touch($rrpath, mkdata($type, $ttl, $rrdata));
    }
    close $zh or carp $!;
    }

    closedir($dh);
    closedir $dh;
  2. mat813 created this gist Jan 12, 2017.
    68 changes: 68 additions & 0 deletions convert.pl
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,68 @@
    #!/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);