Listing02_List_Domains (Perl)

This code can be found in Chapter 7 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::LDAP;

my $server  = 'dc1'; # must be a global catalog server
my $user    = 'administrator@xyz.com';
my $passwd  = 'password';

my $ldap = Net::LDAP->new($server,port=>3268)
           or die "Could not connect to port 3268: $@";

# Need rootDomainNamingContext value from RootDSE
my $rootdse = $ldap->search(base   => '',
                            filter => '(objectclass=*)',
                            scope  => 'base',
                            attrs => ['rootDomainNamingContext']);
die "Could not get RootDSE: ",$rootdse->error if $rootdse->code;
my $base_dn = ($rootdse->entries)[0]->get_value('rootDomainNamingContext');
die "Could not get rootDomainNamingContext from RootDSE\n" unless $base_dn;

# Bind as $user
my $bind = $ldap->bind($user, password => $passwd);
die "Could not bind to server: ", $bind->error if $bind->code;

# Perform search and print results
my $result = $ldap->search(base=>$base_dn,
                           scope=>'subtree',
                           filter=>'(&(objectclass=domainDNS)
                                      (objectcategory=DomainDNS))');
die "Search returned an error: ", $result->error if $result->code;
print $_->get_value('name'),"\n" for $result->entries;

This code has been viewed 854 times.

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