From: Jim H. <ha...@us...> - 2000-08-11 17:27:21
|
Eric, Below is a full, relatively general purpose program that modifies attribues. --Jim Harle On Fri, 11 Aug 2000, Eric Zhou wrote: > hi,everyone: > > I wish to modify an attribute in ldap tree. > I have see the online document about ldap->modify. > > is there a more concrete example about any of the > ldap-> modify() methods. > ________________________________ #!/usr/local/bin/perl use Net::LDAP qw(:all); use Net::LDAP::Constant; use Net::LDAP::Util; use Term::ReadKey; use strict; my $ldap = Net::LDAP->new('directory.somewhere') or die "$@"; print "your login "; chomp ( my $login = <>); print "your passwd "; ReadMode 'noecho'; my $password = ReadLine 0; chomp $password; ReadMode 'normal' ; print "\n"; $ldap->bind ( version=>3) ; #first find dn for this login my $basedn = "o=somewhere"; my $filter = "(|(uid=$login)(cn=$login))"; my $mesg = $ldap->search( base => $basedn, filter => $filter, attrs => ["dn"] ); if ($mesg->code || ($mesg->count() != 1)) { print "Couldn't find $login, message is \n ", Net::LDAP::Util::ldap_error_name($mesg->code), "\n"; exit; } my $entry = $mesg->entry(0); my $admindn = $entry->dn; #print "binding to $admindn\n"; $mesg = $ldap->bind (dn => $admindn, password => $password, version => 3) ; if ($mesg->code) { print "Couldn't bind to $login, message is \n ", Net::LDAP::Util::ldap_error_name($mesg->code), "\n"; exit; } while (1) { print "login for which changes are to be done "; my $who = <>; chomp $who; last unless $who; #find correct dn for this login my $mesg = $ldap->search ( base => $basedn, filter => "(cn=$who)", attrs => ["sn"] ); unless ($mesg->count() ) { print "bad id\n"; next; } die ("multiple IDs\n") if $mesg->count() > 1; my $entry = $mesg->entry(0); my $dn = $entry->dn; while (1) { print "attribute to change "; my $attr = <>; chomp $attr; last unless $attr; print "new value for $attr "; my $value = <>; chomp $value; print "add or replace "; my $a = <>; chomp $a; if (lc(substr($a,0,1)) eq "a") { $mesg = $ldap->modify ($dn, add => {$attr => $value} ); } else { $mesg = $ldap->modify ($dn, replace => {$attr => $value} ); } print Net::LDAP::Util::ldap_error_text($mesg->code),"\nfor $dn\n" if $mesg-> code; } } $ldap->unbind; # take down session |