Listing09_List_Trusts (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';
my $user    = 'administrator@xyz.com';
my $passwd  = 'password';
my $system_cont = "cn=system,dc=xyz,dc=com";

my @trustTypes = ('Unknown','Downlevel','Uplevel','Kerberos','DCE');
my @trustDirections = ('Disabled','Trusting','Trusted',
                       'Trusting and Trusted');

my $ldap = Net::LDAP->new($server) or die "$@";
my $result = $ldap->bind($user, password => $passwd);
$result->code && die $result->error;

my $filter = "(&(objectclass=trustedDomain)(objectcategory=TrustedDomain))";
$result = $ldap->search(base   => $system_cont,
                 scope  => "subtree",
                 attrs  => [ 'trustPartner', 'trustType','trustDirection'],
                 filter => $filter);
$result->code && die $result->error;

print "Trusts for $server:\n";
my $count = 0;
foreach my $entry ($result->entries) {
   $count++;
   my $partner   = $entry->get_value('trustPartner');
   my $type      = $trustTypes[$entry->get_value('trustType')];
   my $direction = $trustDirections[$entry->get_value('trustDirection')];
   print "$count. $partner - $type $direction\n";
}

$ldap->unbind;

This code has been viewed 700 times.

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