Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change

ldap moddn via perl

by murleehyd (Initiate)
on Nov 14, 2017 at 11:08 UTC ( #1203356=perlquestion: print w/replies, xml ) Need Help??
murleehyd has asked for the wisdom of the Perl Monks concerning the following question:

Hi Perl Monks, I am trying to use LDAP moddn to move an user entry from one branch to new branch, see below the script:
# The Script to Migrate User Segment in OUD. use lib "/xxxx/perl/perlmods/share/perl5/"; use lib "/xxxx/perl/perlmods/lib64/perl5/"; use MIME::Base64; use Net::LDAP; # PARAMETERS #ACCEPTANCE: $hostname = "xxxxxx"; $ldapport = "xxxx"; $ldapbindid = "xxxxxx"; $password = "xxxxx"; $ldapbasedn = "ou=mgusers,ou=seg90,ou=cust004,dc=company"; # Connect to the ldap(OUD) instance $LDAP = Net::LDAP->new($hostname, port => $ldapport) or die "$@"; $msg = $LDAP->bind( $ldapbindid, password => $password ); $msg->code && die " >>> LDAP CONNECTION ERROR " . $msg->code . " : " . + $msg->error(); #$msg = $LDAP->moddn( $dn, newrdn => "cn=$cn,ou=users,ou=seg10,ou=cust +006,dc=company" ); open(OUTPUT, ">", "/xxxxx/perl/perlscripts/MIG_USER_OUTPUT.csv"); my $search = $LDAP->search( base => $ldapbasedn, filter => "(uid=*)", attrs => ['cn'] ); $search->code && die " >>> LDAP SEARCH ERROR " . $search->code . " : + " . $search->error(); print OUTPUT "COUNT: ", $search->count; foreach my $entry ($search->entries) { print OUTPUT "Moving user with cn=", $entry->get_value('cn'). + "\n"; #Perform Migration of User segment in OUD my $entry = Net::LDAP::Entry->new; if ($search->entries != 0){ $cn = $entry->get_value('cn'); $dn = "cn=$cn,ou=mgusers,ou=seg90,ou=cust004,dc=company"; $entry = $LDAP->moddn ( $dn, newrdn => "cn=$cn", deleteoldrdn => "1", newsuperior => "ou=users,ou=seg10,ou=c +ust006,dc=company" ); $entry->update($LDAP); } else{ print "Migrate Complete !!! \n";} } END # END block to close the LDAP connections in case we die s +omewhere { if ($LDAP) { $LDAP->unbind(); } }
and I am end up with following error message: Can't locate object method "update" via package "Net::LDAP::ModDN" at line 50, <DATA> line 755. Could you please help on this? what is missing or what/where it is going wrong ? Thank you in advance, Best regards

Replies are listed 'Best First'.
Re: ldap moddn via perl
by kroach (Pilgrim) on Nov 14, 2017 at 14:36 UTC

    I'm not very familiar Net::LDAP and your code is hard to test without a working setup but there are a few suspicious parts in there.

    Why do you assign a new $entry variable in the loop over $search->entries? It covers the list iterator making it unusable, so the loop doesn't make much sense.

    my $entry = Net::LDAP::Entry->new; # covers $entry from `foreach my +$entry (...`

    The moddn() method of Net::LDAP returns a status message, not an LDAP::Search::Entry, that's why you can't use update() on it.

    Add the following at the beginning of your script and try to make it work, you may find some errors this way:

    use strict; use warnings;

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1203356]
Approved by haukex
Front-paged by Corion
What's the matter? Cat got your tongue?...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (2)
As of 2018-02-25 10:31 GMT
Find Nodes?
    Voting Booth?
    When it is dark outside I am happiest to see ...

    Results (312 votes). Check out past polls.