Novell Home

Sample Code: Searching eDirectory using LDAP and Perl

Novell Cool Solutions: Feature

Digg This - Slashdot This

Posted: 29 Sep 2005
 

This sample shows how to use the perl-ldap module to search eDirectory.

You can also find this sample code at:
http://forge.novell.com/modules/xfref_library/detail.php?reference_id=224

Sample Code

#!perl
use Net::LDAP;
use Net::LDAP::LDIF;
use File::Path qw(rmtree);
use File::Basename qw(basename);

print "Content-type: text/html\n\n";

$HOST = '137.65.138.159';
$PORT = 389;
$SSL_PORT = 636;
$PASSWD   = 'novell';
$BASEDN   = "o=novell";
$ADMIN= "cn=admin,o=novell";
$searchstring="(&(objectclass=user)(cn=pjones))";
$attnames=["dn","cn","surname","mail","groupmembership"];

#connect to the server
until($ldap = Net::LDAP->new($HOST, port => $PORT)) {
  die "Can not connect to ldap://$HOST:$PORT/" if ++$count > 10;
  sleep 1;
}

$r = $ldap->bind($ADMIN, password => $PASSWD, version=>3);
die $r->error if $r->code;

$r = $ldap->search(	base 	=> 	$BASEDN,
					scope	=>	'sub',
					filter 	=> 	$searchstring,
					attrs	=>	$attnames);

foreach my $entry ($r->entries){
	print "

dn: ".$entry->dn."
"; my @attrs = $entry->attributes; foreach my $attr (@attrs) { my @value = $entry->get_value($attr); foreach my $value (@value){ print "$attr: $value
"; } } print "

"; } $ldap->unbind;


Novell Cool Solutions (corporate web communities) are produced by WebWise Solutions. www.webwiseone.com

© 2014 Novell