Replay DNS traffic – dnsreplay.pl

The following is a handy little script for replaying DNS traffic and thus verifying that a Name Server will answer as you expect it to.

Updated: Tue Apr 15 16:12:59 PDT 2008.

#!/usr/bin/perl
# Augie Schwer
# dnsreplay.pl - replay a BIND query log and print to STDOUT
# when a query fails against the given name server.
# $Id: dnsreplay.pl 1829 2008-04-15 23:07:12Z augie $

use strict;
use Net::DNS;
use Getopt::Long;

my %options=();

GetOptions( \%options ,
"nameserver=s", "querylog=s", "port=i", "recurse", "help"
);

my $nameserver = $options{'nameserver'};
my $querylog = $options{'querylog'};
my $port = $options{'port'};
my $recurse = $options{'recurse'};
my $help = $options{'help'};

if ( $help ) { print "Usage: dnsreplay.pl --nameserver=NAMESERVER --querylog=QUERYLOG [--port=PORT NUMBER] [--recurse]\n"; exit;}

my $res = Net::DNS::Resolver->new(
'nameservers' => [$nameserver],
'recurse' => $recurse,
'debug' => 0,
'port' => 53 || $port
);

open(FILE,"< $querylog") or die("Could not open $querylog: $!");
while()
{
my ($zone,$type) = (split)[3,5];

if ( $type eq 'A6' ) # Net::DNS does not understand A6 records.
{ print "Skipping A6 Record.\n"; next; }

my $packet = $res->send($zone,$type);

if ( ! defined $packet )
{
warn "Packet not defined for ($zone,$type).\n";
print "Packet not defined for ($zone,$type).\n";
next;
}

if( $packet->answer )
{ print "$nameserver answered for $zone of type $type .\n"; }
else
{ print "$nameserver did not answer for $zone of type $type .\n"; }
}
close(FILE);

To get the Query Log out of BIND put the following in to your named.conf :

logging {
channel queries {
file "query.log";

};
category queries { queries; };
};

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

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>