Previous Topic: Display the Supported Schema

Next Topic: Troubleshooting CA Directory

LDAP Programming in Perl

This section illustrates how to access LDAP data using the Net::LDAP module, which is available from any Perl CPAN archive.

This section assumes that you are familiar with Perl; it does not cover the installation of Perl or the Net::LDAP module. This example is provided without any implied or explicit warranties. CA does not provide support for either the Perl language or the Net::LDAP Perl module.

Note: The line numbers reference the comments (they are not part of the program).

This program extracts entries with the job title Secretary from the Democorp directory, and prints their name and telephone number:

#!/usr/local/bin/perl -w
2	
# This is a demonstration of accessing the Democorp directory using the
# Net::LDAP Perl modules
5	use strict;
7	use warnings;
8	use Net::LDAP;
10
11	my($ldap, $mesg, $entry);
12
13	$ldap = Net::LDAP->new('yourhost.com:19389') or die "Can't connect: $@";
14
15	# anonymous bind
16	$mesg = $ldap->bind;
17	if($mesg->is_error){
18	 die "Bind error: ".$mesg->error;
19	}
20
21	# search for job title = "Secretary"
22	$mesg = $ldap->search(base => "o=Democorp,c=AU",
23			   filter => "(title=Secretary)"
24			   );
25	if($mesg->is_error){
26	 die "Search error: ".$mesg->error;
27	}
28
29	foreach $entry ($mesg->entries) {
30
31	 # report entry's common name (cn) and telephone number
32	 print $entry->get_value('cn'), "\t",
33	    $entry->get_value('telephoneNumber'), "\n";
34
35	}
36
37	print "Finished\n";
38
39	$ldap->unbind;

Comments on LDAP Program

Line 13

Replace yourhost.com with the name of the machine on which the Democorp directory is installed and running.

Line 16

This is an anonymous bind. There is no DN or password supplied.

Line 17

Always check the results of the requests.

Line 22

Execute the query and check for any errors.

Line 29

This loop executes for each entry found by the search. No errors are possible at this point.

Line 32 and 33

Print out the entries found. This usually produces the following output:

Juliet LEVY   524 3637

Craig LINK    544 3697

Terry SALISBURY 488 0288

Winifred LEWIS  534 3657

Ian NATHAN      437 8908

Cheryl APPLEBY  498 0308

Finished

Line 39

Close the connection.