LDAP Perl 脚本查看用户所属的组
我正在尝试编写一个脚本来查询 LDAP 服务器并打印出用户所属的所有组。目前,它仅返回用户所属的条目。
$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";
}
输出:
John Smith smithj [email protected] Employee Office2
CN=DL-Archive Plug In,OU=DistributionList,OU=Messaging,DC=corp,DC=b****n,DC=com
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在列表上下文中使用 get_value 来获取所有值,而不仅仅是第一个值。
例如,
Use get_value in a list context to get all the values instead of just the first value.
For example,