This is my first question on StackOverFlow so please let me know if I have done something wrong.
I am writing a perl script to perform an LDAP search to identify the list groups that a user is a member of. I read the perldocs on the subject and found a few examples on this website but I have been unable to get the script to run properly.
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 tell it seems to me that $entry is not getting any value returned when I run:
$entry = $result->entry($i);
but I could be wrong.
Any help would be appreciated.
Thanks,
The problem is in the attribute part of the search. You give it a string and it is expecting an array ref.
Wrong:
Good: