wtfomgjohnny
wtfomgjohnny

Reputation: 25

LDAP Perl Script to see what groups a user is memberOf

I'm trying to write a script that queries the LDAP server and prints out all the groups that a user is memberOf. Right now it only returns on entry which the user is memberOf.

$sn=$ARGV[0];

$searchbase="OU=All Users,DC=corp,DC=b****n,DC=com";
$filter="(&(sn=$sn)(employeeType=Employee))";
$attrs="sAMAccountName, sn, givenname, l, mail, employeeType, memberOf";

$result = $ldap->search( # perform a search
                   base   => $searchbase,
                   filter => $filter,
                          attrs => $attrs
                 );

$count = $result->count;
print "Count: $count\n";

for ($i=0;$i<$count;$i++) {
$entry=$result->entry($i);
$first=$entry->get_value('givenname');
$last=$entry->get_value('sn');
$location=$entry->get_value('l');   
$email=$entry->get_value('mail');
$login=$entry->get_value('sAMAccountName');
$empType=$entry->get_value('employeeType');
$memberOf=$entry->get_value('memberOf');
#   $=$entry->get_value('');

print "$first $last\t$login\t$email\t$empType\t$location\n";
print "$memberOf\n\n";
}

Output:

John Smith  smithj  [email protected]  Employee    Office2
CN=DL-Archive Plug In,OU=DistributionList,OU=Messaging,DC=corp,DC=b****n,DC=com

Upvotes: 1

Views: 6542

Answers (1)

David Gelhar
David Gelhar

Reputation: 27900

Use get_value in a list context to get all the values instead of just the first value.

For example,

@memberOf=$entry->get_value('memberOf');

Upvotes: 2

Related Questions