Listing07_Verify_All_NS_In_Zone_Same (Perl)

This code can be found in Chapter 8 of Managing Enterprise Active Directory Services

Purchase XP Cookbook or Networking Recipes for only $25 plus shipping! While supplies last.

Find out how to download all of the Perl code from this site.

# From the book "Managing Enterprise Active Directory Services"
# ISBN: 0-672-32125-4

use strict;
use Net::DNS;
use DnsCmd;

# Find all the name servers
my @servers;
my $res = Net::DNS::Resolver->new();
my $query = $res->search("xyz.com", "NS");
if ($query) {
   foreach my $rr ($query->answer) {
      next unless $rr->type eq "NS";
      push @servers,$rr->nsdname;
   }
}
else {
   die "NS query failed: ", $res->errorstring, "\n";
}

# Get /Info for first server in the list
my $ns1      = shift @servers;
my $ns1_obj  = DnsCmd->new($ns1);
my $ns1_info = $ns1_obj->Info;
die $ns1_obj->output if $ns1_obj->error;
my %ns1_hash = parse_info($ns1_info);
print "Comparing $ns1 with:\n";

# Iterate through the other name servers
my $count = 0;
foreach my $ns(@servers) {
   print " $ns\n";
   # Get the info about that server
   my $ns_obj  = DnsCmd->new($ns);
   my $ns_info = $ns_obj->Info;
   if ($ns_obj->error) {
      print "Error getting info for $ns: ",$ns_obj->output,"\n";
      next;
   }
   # parse_info takes the 'Info' string returns a hash of key value
   # pairs consisting of the settings and their values
   my %ns_hash = parse_info($ns_info);
   foreach my $key (sort keys %ns1_hash) {
      if ($ns1_hash{$key} ne $ns_hash{$key}) {
         print "\t$key: $ns1 = $ns1_hash{$key} $ns = $ns_hash{$key}\n";
      }
   }
   $count++;
}
print "$count servers compared\n";

sub parse_info {
my ($info) = @_;
   my %info_hash;
   $info = strip_str($info);
   foreach (split /\n/,$info) {
      /\s*(\w+)\s*=\s*(\w+)/;
      $info_hash{$1} = $2 if defined $1 and defined $2;
   }
   return %info_hash;
}

sub strip_str {
my ($info) = @_;
   $info =~ s/^.+\n  Configuration:\s+(.+)ServerAddresses:.+$/$1/s;
   return $info;
}

This code has been viewed 598 times.

New from the creators of TechTasks.com: StatSheet.com