axfr2rbldnsd – Converting Bind (or anything really) to rbldnsd.

I could not find a script to do this anywhere, so I wrote one; it was meant to convert Bind style zone files to Rbldnsd style zone files, but since it first does an AXFR, it can be used to generate rbldnsd zones from any source that supports DNS transfers.

Here is a direct link to the file, feel free to download it: http://schwer.us/files/axfr2rbldnsd.pl; and here’s the code:


#!/usr/bin/perl

# Author: Augie Schwer
# $Id: axfr2rbldnsd.pl 1274 2007-09-23 20:30:17Z augie $
# Purpose: Convert a transfered zone files to rbldnsd style and
# prints to STDOUT.

use strict;

unless ( defined $ARGV[0] ) { print “USAGE : $0 zone_to_transfer (host)\n”; exit; }

my $zone = $ARGV[0];
my $host = $ARGV[1] || ‘localhost’;
my $time = time();
my $tmp_file = “/tmp/$zone.$time”;
my $found_ns = 0;
my $found_soa = 0;

`dig \@$host axfr $zone > $tmp_file`;

open FILE , “$tmp_file” or die “Could not open $tmp_file : $!”;

while ()
{
# Ignore comments
next if /^;/;

# remove everything in front of SOA, but keep the TTL.
if (!$found_soa) # we only need one SOA.
{
if ( /(\d+)\s+IN\s+SOA\s+(.+)$/ )
{
print “\$SOA $1 $2″;
$found_soa = 1;
next;
}
}

# remove everything in front of NS, but keep the TTL.
# concat all NS records together.
if (!$found_ns and /(\d+)\s+IN\s+NS\s+(.+)$/)
{
print “\n\$NS $1 $2 “;
$found_ns = 1;
next;
}
if ( /IN\s+NS\s+(.+)$/ ) { print “$1 “; next; }

# anything that’s not a sub-domain gets an ‘@’ in
# front followed by the type and record.
if ( /^$zone\.\s+(\d+)\s+IN\s+(A|MX)\s+(.+)$/ ) { print “\n@ $1 $2 $3\n”; next; }
# print the rest of the A records.
if ( /^(.+)\.$zone\.\s+(\d+)\s+IN\s+A\s+(.+)$/ ) { print “$1 $2 IN A $3\n”; next; }
}

# clean up after myself.
`rm -f $tmp_file`;

This entry was posted in General, work and tagged , . Bookmark the permalink.

One Response to axfr2rbldnsd – Converting Bind (or anything really) to rbldnsd.

  1. James says:

    Thanks Augie!

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>