Listing02_Finding_Servers_With_DNS (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;

my $domain = 'subdomain.xyz.com'; # Find DNS and DC servers for this domain
my $forest = 'xyz.com'; # Find the GCs for this forest

my $res = Net::DNS::Resolver->new();

print "Querying for DNS servers in $domain\n";
my $query = $res->search("$domain", "NS");
if ($query) {
   foreach my $rr ($query->answer) {
      next unless $rr->type eq "NS";
      print $rr->nsdname,"\n";
   }
}
else {
   die "NS query failed: ", $res->errorstring, "\n";
}

print "Querying for Domain Controllers in $domain\n";
my $query = $res->search("_ldap._tcp.dc._msdcs.$domain", "SRV");
if ($query) {
   foreach my $rr ($query->answer) {
      next unless $rr->type eq "SRV";
      print $rr->target,"\n";
   }
}
else {
   die "DC query failed: ", $res->errorstring, "\n";
}

print "Querying for Global Catalog servers in $forest\n";
my $query = $res->search("_ldap._tcp.gc._msdcs.$forest", "SRV");
if ($query) {
   foreach my $rr ($query->answer) {
      next unless $rr->type eq "SRV";
      print $rr->target,"\n";
   }
}
else {
   die "GC query failed: ", $res->errorstring, "\n";
}

This code has been viewed 697 times.

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