#!/usr/bin/perl
################################################## ####################################
# scan_dns.pl version 0.8 7/14/09
# Author: Warren Watts (kingbeetle_66(at)yahoo(dot)com
# This program will analyze the relative speeds of each of the DNS servers
# detected by dhcpcd (DHCP client daemon) through use of dig (a DNS lookup utility)
# and display a sorted list of DNS servers from fastest to slowest.
# This sorted list can then be used to tweak the /etc/resolv.conf file, providing
# faster DNS lookups while browsing, etc.
# I included three domains for dig to lookup. They are stored in @domain_list.
# Feel free to add to the list or change the domain list to any domains you wish.
# You can also pass edditional DNS servers to be tested to the script at the command line:
# EXAMPLE: perl scan_dns.pl 192.168.1.234 70.82.32.232
# Be aware that the more domains in the list, the longer the scan will take!
# Copyright 2007,2009 Warren B. Watts
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
################################################## #####################################
use strict;
my (@DNS,$parm,$bad_format,$IP,$time,$domain,$dig,%ti me,$key);
# Read the DNS list from /etc/resolv.conf and store the list in an array
my $resolv = `cat /etc/resolv.conf`;
while ($resolv =~ /nameserver (.*)\n/g) {push(@DNS,$1)}
# Check for additional DNS servers to be tested that might have been entered at the command line
# and add them to the array of servers to be tested
if (@ARGV)
{
# if the DNS server address has a valid format, and all the octets are within range, add the DNS server
# to the array of servers to be tested, otherwise display an error
foreach $parm (@ARGV)
{
undef($bad_format);
if ($parm =~ m/^(\d\d?\d?)\.(\d\d?\d?)\.(\d\d?\d?)\.(\d\d?\d?)/ )
{
if($1 <= 255 && $2 <= 255 && $3 <= 255 && $4 <= 255) {push(@DNS,$parm)}
else {$bad_format = 1}
}
else {$bad_format = 1}
if ($bad_format)
{
print "\n+---------------------------------- ERROR --------------------------------+\n";
print("DNS server $parm is not in a valid format and will not be tested.\n");
print "+---------------------------------- ERROR --------------------------------+\n";
}
}
}
#List the DNS servers to be tested
print "\n+-------------------------------------------------------------------------+\n";
print "The following DNS servers will be tested:\n";
foreach $IP (@DNS) {print "$IP\t"}
print "\n";
# Store list of domains to be looked up by dig in an array
my @domain_list = ('www.yahoo.com' ,'www.fasthit.net' ,'zz.nullwave.ru');
# List the domains to be used by dig during the scan
print "The following domains will be tested in this scan:\n";
foreach $domain (@domain_list) {print "$domain\t"}
print "\n";
print "+-------------------------------------------------------------------------+\n";
# Count number of domains stored in the array
my $domain_count = @domain_list;
# Go through the list of DNS servers and execute dig for each DNS server and each
# domains in the domain list, extract the time taken and average the times together,
# then store the DNS and averaged time in a hash.
foreach $IP (@DNS)
{
print "Scanning $IP\n";
$time = '';
foreach $domain (@domain_list)
{
print "--> $domain\n";
$dig = `dig \@$IP $domain`;
if ($dig =~ /Query time: (.*) msec/)
{
$time = $time + $1;
}
}
$time{$IP} = int(($time / $domain_count) +0.5);
}
print "+-------------------------------------------------------------------------+\n";
# Display the results sorted by time in ascending order
foreach my $key (sort hashValueAscendingNum (keys(%time)))
{
print "Average fetch time for $key : $time{$key} msec\n";
}
print "\n";
# Subroutine to sort by time rather than by DNS address
sub hashValueAscendingNum
{
$time{$a} <=> $time{$b}
}
Bookmarks