Skip to Main Content
Perl in a Nutshell, 2nd Edition
book

Perl in a Nutshell, 2nd Edition

by Nathan Patwardhan, Ellen Siever, Stephen Spainhour
June 2002
Beginner content levelBeginner
759 pages
80h 42m
English
O'Reilly Media, Inc.
Content preview from Perl in a Nutshell, 2nd Edition

Adding an Entry to the Directory with Net::LDAP

Now that you’ve searched the directory with Net::LDAP, let’s add an item. In the previous example, you bound to the directory anonymously, but when writing changes to the directory (that is, adding or deleting entries), you’ll probably have to bind as an administrator or another user delegated with similar privileges. In this example, we’ll use “Directory Manager” as the directory administrator.

Adding to a directory with Net::LDAP is a two-phase process:

  1. Bind to the directory as a user who has privileges to write to the directory. In this instance, we’ll use “Directory Manager”.

  2. Use Net::LDAP::add( ) to add the entry. add( ) takes the parts of the record that you will add to the directory as arguments.

This example, based on the following LDIF, uses add( ) to add an account for 'nvp' to the directory:

dn: uid=nvp,ou=People,o=your.domain
uid: nvp
cn: Nathan Patwardhan
givenname: Nathan
sn: Patwardhan
objectClass: person
objectClass: organizationalPerson
objectClass: account
objectClass: shadowAccount
objectClass: top
userPassword: {crypt}/-password-
loginShell: /usr/bin/bash
uidNumber: 1000
gidNumber: 1000
homeDirectory: /users/nvp

Here’s the code:

my $admin = q[cn=Directory Manager]; my $ad_pw = q[adminpass]; my $lsvr = q[ldap.my.domain]; my $org = q[o=my.domain]; my $o_ou = q[ou=People]; my $o_act = q[uid=nvp]; my $ldap = Net::LDAP->new($lsvr); # Bind to a directory with DN and password $ldap->bind($admin, password => $ad_pw); my $l_rec ...
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.
Start your free trial

You might also like

Perl by Example, Fourth Edition

Perl by Example, Fourth Edition

Ellie Quigley
Perl Cookbook, 2nd Edition

Perl Cookbook, 2nd Edition

Tom Christiansen, Nathan Torkington
Perl in a Nutshell

Perl in a Nutshell

Nathan Patwardhan, Ellen Siever, Stephen Spainhour
Learning Perl, 7th Edition

Learning Perl, 7th Edition

Randal L. Schwartz, brian d foy, Tom Phoenix

Publisher Resources

ISBN: 0596002416Errata Page