我有这个代码
#!/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"; }
使用正则表达式输出CN:
aaaa bbbb cccc ...
使用Dumper可以看到结构
$VAR1 = [ 'CN=aaaa,OU=test,DC=test,DC=example,DC=com','CN=bbbb,'CN=cccc,
所以我想知道是否有更多“LDAP”方式来提取这些CN,而不是使用正则表达式?
更新:
根据Javs的回答,这是解决方案.
my $ref = $mesg->entry->get_value("member",asref => 1); foreach my $string (@{$ref}) { print ldap_explode_dn($string)->[0]{CN} . "\n"; }
解决方法
您可以:
use Net::LDAP::Util qw(ldap_explode_dn);
并在你的属性上使用它,如下所示:
ldap_explode_dn($mesg->entry->get_value('member'));
得到这个哈希数组:
$VAR1 = [ { 'CN' => 'aaaa' },{ 'OU' => 'test' },{ 'DC' => 'test' },{ 'DC' => 'example' },{ 'DC' => 'com' } ];