Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

newbie perl ldap question 2

Status
Not open for further replies.

Nostradamus

Technical User
Joined
May 3, 2000
Messages
419
Location
SE
I'm trying to do the following.

Connect to an ldap server and modify an object (objectclass=groupofuniquenames).
The object has an attribute called uniquemember and it's value (multivalued attribute) are several distinguished names (DN).

I want to update these uniquemembers with a list of dn's that I have in an external file.

I'm no programmer and after this day, I'm sure I'll never will be.

I manage to connect without any problem. I can also do ldap-modify to delete and add values of my attribute.
BUT, only if I specify them manually. I can't manage to change my values automatically from the input file.

This is my code.

Code:
my $DISTNAME = 'cn=admin,ou=unit,o=organisation,l=locality,c=country';

open (FILE, "dn");

        while (<FILE>) {
        my $diffdn = "$_";
        print $diffdn;
        $ldap->modify( $DISTNAME, add => {uniqueMember => '$diffdn'} );
}
close FILE;
It prints my entries beautifully but I can't get is

What I want to do is run ldap->modify on the admingroup and add uniquemember from the input-file called dn.
I know I'm close and that this is easily fixed. However I've spent the whole day trying to find a sollution to this. I've searched the internet and changed the code way to many times.

Any help on this would be very grateful.

/Sören
 
could be the single-quotes around '$diffdn' are causing your code to not work correctly, remove them. Don't use any quotes around a single-scalar:

Code:
$ldap->modify( $DISTNAME, add => {uniqueMember => $diffdn} );

Then try your code again.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
I've tried all sorts of quotas around variables and commands without success. I tried removing the single ones from the command, but unfortunately it didn't help.

Do you see anything else that's different or wrong?

Am I doing it right when I extrakt the $_ from the input and use that as input to my ldap-modify request? Will it continue automatically with the next line in my input file using the above code?
I looked into foreach and other ways using the while statement to get it to work but I don't know what I'm doing.

I appreciate every input on this.
Thanks in advance.

/Sören
 
Does it print $diffdn to the screen every time?

add some error catching to your code to
$ldap->modify( $DISTNAME, add => {uniqueMember => '$diffdn'} ) or die "can't do blah : $!\n";

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those who say it cannot be done are usually interrupted by someone else doing it; Give the wrong symptoms, get the wrong solutions;
 
thanks for your response.

The reason i print $diffdn is to see if it list the content of my input files. which it does. The input file is fairly big/long so I commented it out so it wouldn't interfere with the errorcodes. However the command doesn't die so the $! doesn't show anything. I guess the command is working properly then?
But it still doesn't do what I want.

Any other ideas?

/Sören
 
Though not knowing that module, I would suggest three points for consideration:
- [tt]$diffdn[/tt] in your code contains an end of line; try replacing the three lines from [tt]while[/tt] with[tt]
my$diffdn;
while($diffdn=<FILE>){
print$diffdn;
chomp$diffdn;[/tt]
-when the argument to [tt]add[/tt] is multivalued, you should pass a reference to an array of values, not a single string; you obtain such a reference by doing (assuming your values are space separated):[tt]
my@names=split(/\s/,$diffdn);
$ldap->modify($DISTNAME,add=>{uniqueMember=>\@names});[/tt]
-I see in the documentation that [tt]modify[/tt] (and other methods) return a message object that you can use to know why the operation was not performed


Franco
: Online engineering calculations
: Magnetic brakes for fun rides
: Air bearing pads
 
I really appreciate your input. Thanks a lot.

I've tried changing to the code you wrote. No luck.
There must be something wrong with the setup of variables from the input file.

This works like a charm.
Code:
my $DISTNAME = 'cn=admin,ou=unit,o=organisation,l=locality,c=country';

my $DISTNAME2 = 'cn=test test123,ou=unit1,o=organisation,l=locality,c=country'; 

$ldap->modify( $DISTNAME, add => {uniqueMember => $DISTNAME2} )
As soon as I want to take entries line for line from the input file it's not working. To me that sounds like I don't have problem with the attribute beeing multivalued?

The inputfiles (called dn) looks something like this.

cn=test test123,ou=unit1,o=organisation,l=locality,c=country
cn=test2 test234,ou=unit3,o=organisation,l=locality,c=country
cn=test3 test1,ou=unit2,o=organisation,l=locality,c=country
and so on...

If I print @names using your code (after chomp and split) it shows as one long line. Can I really use that as input the way I want?

What should I type to get the return messages from the modify operation?
I can't figure out why the command doesn't give me anything on standard output. If I only knew the errorcodes (if any) from the command, I could figure out how to feed the operation properly.

sigh. hope you understand what I mean.


/Sören
 
From the online Net::LDAP documentation you should do something like this:[tt]
$result=$ldap->modify($DISTNAME,add=>{uniqueMember=>$diffdn});
$result->code&&warn"failed to modify entry: ",$result->error;[/tt]
Can't see any reason why your last code works and it doesn't when you read from the file.

Franco
: Online engineering calculations
: Magnetic brakes for fun rides
: Air bearing pads
 
Code:
my $DISTNAME
   = 'cn=admin,ou=unit,o=organisation,l=locality,c=country';
open (FILE, "dn") or die "File open failed: $!\n";
while (<FILE>) 
  {
  chomp;
  $result = $ldap->modify( $DISTNAME, 
     add => {uniqueMember => $_} );
  $result->code && 
     warn "failed to modify entry: ",$result->error;
  }
close FILE;

'$distdn' is a string literal and has nothing to do with the value of $distdn, it is merely the dollar sign followed by distdn.

Kordaff
 
prex1: I added the errorcodes which was very informative *star* Thanks.

kordaff: I've tried using $_ is input before but I never got it to work. I don't have my original code left for comparison, but it really doesn't matter since the code you posted do what I want. It reads line by line from the file and adds the attribute plus showing errors if any.
Thanks a lot! *star*

/Sören
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top