perlldapnetldap

use file for filter in Perl Net::LDAP


when i'm doing an LDAPsearch manually on the commandline, i can do it so that the filter attribute can come off a file like this :

ldapsearch -v -h <host> -D "<DN>" -b "OU=myou,DC=mydc" -f myqueries.txt "(cn=%s)"

myqueries.txt contains entries such as :

name1
name2
nameN

now i'm trying to do the same thing in Perl using Net::LDAP and i couldn't find any option like that in the documentation. do you know if it can do it ?

in the worst case, i know that i can probably create an array of records (containing queries) and make a loop with many ldapsearches on these names, it will work, but i'd prefer doing something easier with a net::ldap attribute if it is possible (less code)

e.g.

$data = $ldap->search(
     base   => $dn,
     scope  => 'one',
     pagesize  => '999',
     filter => '(cn=%s)',
     file => 'myqueries.txt',                # this option does not exist
     attrs  => [ qw( displayName cn ) ]
  );

thanks !


Solution

  • It seems that ldapsearch -f option is not implemented in Perl Net::LDAP, but you can still do the exact equivalent that is : performing a search operation for each line read from an input file :

    open $handle, '<', $filepath;
    chomp(@lines = <$handle>);
    close $handle;
    
    foreach $cn (@lines) {
      $data = $ldap->search(
         base   => $dn,
         scope  => 'one',
         pagesize  => '999',
         filter => "(cn=$cn)",
         attrs  => [ qw( displayName cn ) ]
      );
      ...
    }
    

    You can also avoid the foreach loop and still get the same results in one single query by concatenating the attribute values in a OR filter :

    $filter = '(|(cn=' . join(')(cn=', @lines) . '))';