开发者

Extracting CN's with LDAP?

开发者 https://www.devze.com 2023-01-30 02:18 出处:网络
I have this code #!/usr/bin/perl use warnings; use strict; use Net::LDAP; use Data::Dumper; my $dn=\"CN=...\";

I have this code

#!/usr/bin/perl

use warnings;
use strict;
use Net::LDAP;
use Data::Dumper;

my $dn="CN=...";
my $password="xxx";

my $ldap = Net::LDAP->new('example.com') or die "$@";
my $mesg = $ldap->bind($dn, password=>$password);
if ($mesg->code) { die "uuuu $mesg"; }

$mesg = $ldap->search(base => "dc=test,dc=example,dc=com", filter => "(name=LIST)",);

my $ref = $mesg->entry->get_value("member", asref => 1);
print Dumper $ref;

foreach my $string (@{$ref}) {
    $string =~ /CN=(.+?),.*/;
    print $1 . "\n";
}

which outputs the CN's using regular expressions:

aaaa
bbbb
cccc
...

Using Dumper can I see the structure

$VAR1 = [
          'CN=aaaa,OU=test,DC=test,DC=example,DC=com',
          'CN=bbbb,OU=test,DC=test,DC=example,DC=com',
          'CN开发者_运维百科=cccc,OU=test,DC=test,DC=example,DC=com',

So I am wondering if there is a more "LDAP" way to extract these CN's, instead of using regular expressions?

Update:

Based on Javs answer this is the solution.

my $ref = $mesg->entry->get_value("member", asref => 1);

foreach my $string (@{$ref}) {
    print ldap_explode_dn($string)->[0]{CN} . "\n";
}


You can:

use Net::LDAP::Util qw(ldap_explode_dn);

and use it on your attribute like this:

ldap_explode_dn($mesg->entry->get_value('member'));

to get this array of hashes:

$VAR1 = [
      {
        'CN' => 'aaaa'
      },
      {
        'OU' => 'test'
      },
      {
        'DC' => 'test'
      },
      {
        'DC' => 'example'
      },
      {
        'DC' => 'com'
      }
    ];


You do realize that CN is usually an attribute in LDAP directories?

Why not just query for the attribute CN for all returned objects? Then no parsing required.

0

精彩评论

暂无评论...
验证码 换一张
取 消