Martin,

thanks for you quick reply!

I now tried like this, but it still does not quite work:


#!/usr/bin/perl

use Net::LDAP;
use Net::LDAP::Control::Paged;
use Net::LDAP::Constant qw( LDAP_CONTROL_PAGED );

my $bindDn = 'username';
my $bindPwd = 'password';
my $ldaphost = "ldaphost";
# connect and bind with error handling
my $ldap = Net::LDAP->new( $ldaphost, port=>10999 )
    or die "Error in connecting to '$ldaphost': $@";
my $rc = $ldap->bind( $bindDn, password => $bindPwd );
if( $rc->code ) {
PrintLdapError( $ldap, $rc, "Error in bind as '$bindDn'" ); # error handling
}

my $pagedControl = Net::LDAP::Control::Paged->new( size => 50 );

my @searchArgs = (
    control => [ $pagedControl ],
    base     => "ou=users,o=Broadband,o=no",
    scope    => "subtree",
    filter   => "(objectClass=top)",
);

my $cookie;
while(1) {

    print "=" x 60, "\nNext page\n", "=" x 60, "\n";
    $pagecount++;
    $entrycount=0;

     my $searchResult = $ldap->search( @searchArgs );

    $searchResult->code and last; # only continue at LDAP_SUCCESS

    # do something with the found entries
    while( my $entry = $searchResult->shift_entry ) {
        #$entry->dump;
        $entrycount++;
    }
    print "page $pagecount: Entries: $entrycount\n";

    # get Cookie from Paged Control
    my( $response ) = $searchResult->control( LDAP_CONTROL_PAGED )
        or last;
    my $cookie = $response->cookie or last;


    # set cookie for next search
    $pagedControl->cookie( $cookie );

} # while 1

# if cookie is set => error, tell server that search is finished
if( $cookie ) {
    $pagedControl->cookie( $cookie );
    $pagedControl->size( 0 );
    $ldap->search( @searchArgs );
} # if

$ldap->unbind();

sub PrintLdapError { # default error handling, needs to be improved
    my( $ldap, $rc, $msg ) = @_;

    my $name  = ldap_error_name( $rc );
    my $text  = ldap_error_text( $rc );
    my $descr = ldap_error_desc( $rc );

    die join "\n", $msg, "Name: $name", "Descr: $descr", "Text: $text", '';
} # PrintLdapError



Running it gives me this output:
# perl testpages3.pl
============================================================
Next page
============================================================
page 1: Entries: 206





On Wed, 02 Apr 2008 10:13:02 +0200, Martin Fabiani <[EMAIL PROTECTED]> wrote:

 use Net::LDAP::Control::Paged;
 use Net::LDAP::Constant qw( LDAP_CONTROL_PAGED );
# connect and bind with error handling
 my $ldap = Net::LDAP->new( "ldaphost", port=>10999 )
     or die "Error in connecting to 'ldaphost': $@;
 my $rc = $ldap->bind( $bindDn, password => $password );
 if( $rc->code ) {
PrintLdapError( $ldap, $rc, "Error in bind as '$bindDn'" ); # error handling
 }
my $pagedControl = Net::LDAP::Control::Paged->new( size => 500 );
my @searchArgs = (
     control => [ $pagedControl ],
     base     => "ou=users,o=Broadband,o=no",
     scope    => "subtree",
     filter   => "(objectClass=top)",
 );
my $cookie;
 while(1) {
    print "=" x 60, "\nNext page\n", "=" x 60, "\n";
     my $searchResult = $ldap->search( @searchArgs );
    $searchResult->code and last; # only continue at LDAP_SUCCESS
    # do something with the found entries
     while( my $entry = $searchResult->shift_entry ) {
         $entry->dump;
     }
    # get Cookie from Paged Control
     my( $response ) = $searchResult->control( LDAP_CONTROL_PAGED )
         or last;
     my $cookie = $response->cookie or last;
    # set cookie for next search
     $pagedControl->cookie( $cookie );
} # while 1
# if cookie is set => error, tell server that search is finished
 if( $cookie ) {
     $pagedControl->cookie( $cookie );
     $pagedControl->size( 0 );
     $ldap->search( @searchArgs );
 } # if
$ldap->unbind();
sub PrintLdapError { # default error handling, needs to be improved
     my( $ldap, $rc, $msg ) = @_;
    my $name  = ldap_error_name( $rc );
     my $text  = ldap_error_text( $rc );
     my $descr = ldap_error_desc( $rc );
die join "\n", $msg, "Name: $name", "Descr: $descr", "Text: $text", '';
 } # PrintLdapError



--
Using Opera's revolutionary e-mail client: http://www.opera.com/mail/

Reply via email to