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


Copyright © 2009 CA. All rights reserved. Email CA about this topic