Listing08_Server-side_Sort_And_Paged_Net-LDAP (Perl)

This code can be found in Chapter 3 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
# Copyright (C) 2002 by Addison-Wesley
# Script by Robbie Allen
# 
# -------------------------------------------------------------------
use Net::LDAP;
use Net::LDAP::Control;
use Net::LDAP::Constant qw( LDAP_CONTROL_PAGED LDAP_CONTROL_SORTREQUEST );
$ldap = Net::LDAP->new('dc1.xyz.com', port=>3268)
   or die "Could not connect: $@";
$search = $ldap->bind('administrator@xyz.com', password => 'password');
die $search->error if $search->code;
$page = Net::LDAP::Control->new( LDAP_CONTROL_PAGED, size => 100);
$sort = Net::LDAP::Control->new( LDAP_CONTROL_SORTREQUEST, order => 'sn');
@args = ( base  => "cn=users,$base_dn",
          scope => "subtree",
          filter => "(&(objectClass=user)(objectCategory=Person))",
          control => [ $sort, $page ] );
while (defined ($mesg = $ldap->search( @args )) ) {
      print "Count: ",$mesg->count,"\n";
      foreach $entry ($mesg->entries) {
         print $entry->get_value('cn'),"\n";
      }
      ($resp) = $mesg->control( LDAP_CONTROL_PAGED );
      last unless $page->cookie($resp->cookie);
}
$ldap->unbind;

This code has been viewed 836 times.

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