2010-12-10 101 views
0

當我做在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 

有誰知道該怎麼做?

+1

使用[GQ](http://sf.net/projects/gqclient/)LDAP客戶端撰寫和測試查詢。 – daxim 2010-12-10 15:18:53

+2

'(objectCategory = CN = Person,CN = Schema,CN = Configuration,DC = example,DC = com)'是一個有效的LDAP過濾器,您可以使用'&'運算符將其與另一個過濾器組合: (過濾器1)(過濾器2))' – hobbs 2010-12-11 03:45:19

回答

1
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,只需在最裏面的塊中打印一張。