Listing07_Server-side_Sort_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_SORTREQUEST );
$ldap = Net::LDAP->new('dc1.xyz.com')
    or die "Could not connect: $@";
$search = $ldap->bind('administrator@xyz.com', password => 'password');
die $search->error if $mesg->code
$sort = Net::LDAP::Control->new( LDAP_CONTROL_SORTREQUEST, order => 'cn');
$search = $ldap->search(
             base  => "cn=users,dc=xyz,dc=com",
             scope => "subtree",
             filter => "(&(objectClass=user)(objectCategory=Person))",
             control => [ $sort ] );
print $entry->get_value('cn'),"\n" foreach $entry $search->entries;
$ldap->unbind;

This code has been viewed 729 times.

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