在LDAP中搜索条件?
问题描述:
当我做在LDAP中搜索条件?
#!/usr/bin/perl -w
use strict;
use Net::LDAP;
use Data::Dumper;
my $dn="...";
my $password="xxx";
my $ldap = Net::LDAP->new('example.com') or die "[email protected]";
my $mesg = $ldap->bind($dn, password => $password);
if ($mesg->code) { die "uuuu $mesg"; }
$mesg = $ldap->search(
base => "dc=example,dc=com",
filter => "(name=LIST)",
);
print Dumper $mesg;
我得到
$VAR1 = bless({
'parent' => bless({
...
}, 'Net::LDAP'),
'entries' => [
bless({
'changes' => [],
'changetype' => 'modify',
'asn' => {
'objectName' => 'CN=LIST,OU=test group,OU=M,OU=I,DC=example,DC=com',
'attributes' => [
{
'type' => 'objectClass',
'vals' => [
'top',
'group'
]
},
{
'type' => 'cn',
'vals' => [
'LIST'
]
},
{
'type' => 'member',
'vals' => [
'CN=user1,OU=BaseUsers,DC=example,DC=com',
'CN=user2,OU=BaseUsers,DC=example,DC=com',
]
},
...
在这里我只想输出来自member
那些在他们的对象
objectCategory: CN=Person,CN=Schema,CN=Configuration,DC=example,DC=com
有谁知道该怎么做?
答
foreach my $entry (@{$mesg->{'entries'}})
{
my $match = 0;
my $name = $entry->{'asn'}->{'objectName'};
foreach my $attr (@{$entry->{'asn'}->{'attributes'}})
{
if('member' eq $attr->{'type'})
{
foreach my $val (@{$attr->{'vals'}})
{
if($val =~ /^CN=.*,CN=.*,CN=.*,DC=example,DC=com$/)
{
$match = 1;
last;
}
}
}
}
if($match)
{
print $name;
}
}
对于上面的示例数据,由于没有“成员”匹配您指定的搜索模式,因此将返回任何匹配项。此外,我不确定是否要输出对象名称(按照我的代码)或匹配的字符串。如果后者不需要$match
,只需在最里面的块中打印一张。
使用[GQ](http://sf.net/projects/gqclient/)LDAP客户端撰写和测试查询。 – daxim 2010-12-10 15:18:53
'(objectCategory = CN = Person,CN = Schema,CN = Configuration,DC = example,DC = com)'是一个有效的LDAP过滤器,您可以使用'&'运算符将其与另一个过滤器组合: (过滤器1)(过滤器2))' – hobbs 2010-12-11 03:45:19