Failed to process perl LDL request

This is my first question at StackOverFlow, so please let me know if I did something wrong.

I am writing a perl script to perform an LDAP search to identify the list groups of which the user is a member. I read perldocs on this subject and found some examples on this site, but I was not able to run the script.

Here is my code:

#!/usr/bin/perl use strict; use warnings; use Net::LDAP; my $ldap; my $ldapuser = "MVS\\user"; my $ldappass = "passw0rd"; my $searchbase = "DC=dom,DC=site,DC=labs,DC=domain,DC=net"; my $attrs = "sAMAccountName, sn, givenname, memberOf"; my $filter="(samAccountName=user)"; my $result; # ensure the ldap connection is made print "Connecting to LDAP...\n"; $_ = $ldap = Net::LDAP->new('dc1.mvs.cso.labs.rim.net') or die " $@ "; # ensure the bind is successful print 'Binding....'; $_ = $ldap->bind ("$ldapuser", password=>$ldappass, version=>3); print $_->error_text(); $result = $ldap->search(base=>"$searchbase", filter=>"$filter", attrs=>[$attrs]); my $max; my $i; my $entry; my $first; my $last; my $login; my @memberOf; $max = $result->count; print "Count: $max\n"; for ($i=0;$i<$max;$i++) { $entry = $result->entry($i); $last = $entry->get_value('sn'); $login = $entry->get_value('sAMAccountName'); @memberOf = $entry->get_value('memberOf'); print "$first $last\n"; print "$login\n"; print "@memberOf\n"; } $ldap->unbind; $ldap->disconnect; 

Here is the result when I run this code:

 Connecting to LDAP... Binding....Operation completed without error Count: 1 Use of uninitialized value $first in concatenation (.) or string at ./ldap_test.cgi line 43, <DATA> line 604. Use of uninitialized value $last in concatenation (.) or string at ./ldap_test.cgi line 43, <DATA> line 604. Use of uninitialized value $login in concatenation (.) or string at ./ldap_test.cgi line 44, <DATA> line 604. 

From what I can say, it seems to me that $ entry does not get any value returned at startup:

 $entry = $result->entry($i); 

but I could be wrong. Any help would be appreciated.

Thanks,

+4
source share
1 answer

The problem is the attribute part of the search. You give it a string and it expects an array of ref.

Wrong:

 my $attrs = "sAMAccountName, sn, givenname, memberOf"; $result = $ldap->search(base=>"$searchbase", filter=>"$filter", attrs=>[$attrs]); 

Good:

 my $attrs = ["sAMAccountName", "sn", "givenname", "memberOf"]; $result = $ldap->search(base=>"$searchbase", filter=>"$filter", attrs=>$attrs); 
+4
source

Source: https://habr.com/ru/post/1443006/


All Articles