20070313 simple paged results with netldap - plembo/onemoretech GitHub Wiki
title: Simple Paged Results with Net::LDAP link: https://onemoretech.wordpress.com/2007/03/13/simple-paged-results-with-netldap/ author: lembobro description: post_id: 739 created: 2007/03/13 21:24:00 created_gmt: 2007/03/13 21:24:00 comment_status: open post_name: simple-paged-results-with-netldap status: publish post_type: post
Simple Paged Results with Net::LDAP
Not sure if I’ve posted on this before, but I wanted to capture it now. Useful for doing big searches against an Active Directory (default restriction on search results returned is 200). Using Net::LDAP.
#!/usr/bin/perl
use Net::LDAP;
use Net::LDAP::Entry;
use Net::LDAP::Control::Paged;
use Net::LDAP::Constant qw( LDAP_CONTROL_PAGED );
my $adHost = "detroit.domain.com"
my $adUsr = "cn=ldapadmin,cn=users,dc=company,dc=domain,dc=com";
my $adPass = "1234xyz";
my $base = "cn=users,dc=company,dc=domain,dc=com";
my $query = "(mail=*)";
my @attrs = qw(cn displayname mail);
my $ldap = Net::LDAP->new($adHost) or die $!;
my $mesg = $ldap->bind($adUsr, password =>$adPass);
my $page = Net::LDAP::Control::Paged->new( size => 1000 ) or die $!;
my @args = ( base => $base,
scope => 'sub',
filter => $query,
attrs => @attrs,
control => [ $page ],
);
my $cookie;
while (1) {
$mesg = $ldap->search ( @args ) or die $!;
while (my $entry = $mesg->shift_entry()) {
my $entrydn = $entry->dn();
my $mail = $entry->get_value('mail');
my $displayname = $entry->get_value('displayname');
my $cn = $entry->get_value('cn');
print ""$displayname","$mail"n";
}
my ($resp) = $mesg->control(LDAP_CONTROL_PAGED) or last;
$cookie = $resp->cookie or last;
$page->cookie($cookie);
}
if ($cookie) {
$page->cookie($cookie);
$page->size(0);
$ldap->search( @args );
}
$ldap->unbind;
Copyright 2004-2019 Phil Lembo