Novell Home

Persistent search in Perl

From Developer Community

This is code to do a persistent search to an eDirectory server in Perl, using perl-ldap.

Full Script

#!/usr/bin/perl
#
#persist.pl -- Persistent search with Net::LDAP
#
#


use Net::LDAP;
use Net::LDAP::Control;
use Convert::ASN1;

$ldap=Net::LDAP->new("localhost");

$ldap->bind("cn=admin,o=novell",password=>"novell");

$asn=Convert::ASN1->new;

$asn->prepare(q<
                PersistentSearch ::= SEQUENCE {
                   changeTypes INTEGER,
                   changesOnly BOOLEAN,
                   returnECs BOOLEAN
           }

>);


$params=$asn->encode( 	changeTypes=>15, 
			changesOnly=>0, 
			returnECs=>0);

$persistctl=Net::LDAP::Control->new(
			critical=>true,
			type=>"2.16.840.1.113730.3.4.3",
			value=>$params);

$result=$ldap>search(
			base=>"o=novell",
			filter=>"(&(groupMembership=cn=users,o=novell)(ou:dn:=appnotes))",
			control=>$persistctl,
			callback=>\&get_result);

if ($result->code) {
        $error = $result->code;
        $errstr = $result->error;
        print "Error: $error ($errstr)\n";
        }


sub get_result {
        my ($message,$entry) = @_;
        if ( !defined($entry)) {
                print "No records found.\n";
                return;
        }
        my $dn=$entry->dn;

        my $surname=$entry->get_value('surname');
        print "added to group:  $dn, $surname\n";
        }

$result->pop_entry;

--mrichich

Novell® Making IT Work As One

© 2008 Novell, Inc. All Rights Reserved.