Connecting, Binding, and Searching
To get started with the Net::LDAP module, we
will write a basic LDAP query script named
search.pl
. This script illustrates the methods
used to connect to a directory server and retrieve information. It
begins by importing the Net::LDAP symbols via the
use pragma:
#!/usr/bin/perl use Net::LDAP;
After the module has been included, you can create a new instance of a Net::LDAP object. To create a new Net::LDAP instance, you need the hostname of the LDAP server to which the script should connect. The constructor allows several optional arguments, of which the most common and useful are:
-
port The TCP port on which the directory server is listening. If this parameter is not defined, it defaults to the well-known LDAP port (389).
-
version The LDAP version to be used when connecting to the server. The default is Version 2 in the 0.26 release. However, this is likely to change in the future. Always explicitly set the version parameter if your Perl program replies with LDAPv3 features (such as SASL or referrals).
-
timeout The time in seconds that the module should wait when contacting the directory server. The default value of 120 seconds is sufficient for most situations, but for more complex searches or when communicating with a very large directory, it may be necessary to increase this value.
The next line of code establishes a connection to the host ldap.plainjoe.org on port 389 using Version 3 of the protocol. The returned value is a handle to a Net::LDAP ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access