How to query Organizational Units from a given DN in Perl

7.1k views Asked by At

I need to retrieve all Organizational Units from a given DN stringh, I am using Net::LDAP module and this little script:

my $msg = $ldap->search(
    base=>'DC=sample1,DC=sample2',
    filter=>'(objectclass=User)',
);
foreach $entry ($msg->entries) {
    $dn = $entry->dn;
    #how can i retrieve OUs?
}

For example if dn returns that string:

CN=Sample Sample,OU=One,OU=Two,DC=sample1,DC=sample2

I want to retrieve One and Two.

2

There are 2 answers

2
Terry Gardner On BEST ANSWER

Issue a one level search request using the base object dc=example1,dc=sample2 and a presence filter of (ou=*). Given those results, issue a one level search using each returned ou with a presence filter of (ou=*). For each of these searches, specify a size limit and a time limit. For more information on search requests, see "LDAP: Using ldapsearch" and "LDAP: programming practices".

1
dorancemc On

Most detailed "(&(ou=*)(objectClass=organizationalunit))"