Re: Net::LDAP compare question



max_headroom27606@xxxxxxxxx wrote:
I am writing a perl script to query an ldap database and find users who
do not belong to any mail distribution list. I can run the query just
fine, but my problem is getting only results back for users who do have
an attribute entry for the field "memberOf". Here is my code
listing....


I'm not very familiar with Net::LDAP and, in any case, I don't have access to an LDAP right now on which to test. But I think you should rule out the possibility that you're getting problems by writing less than optimal Perl code.

use strict; # this will require you to declare all variables with 'my'
use warnings;

$ldap = Net::LDAP->new( '**************' );

$mesg = $ldap->bind ( "$user",

# unnecessary stringification: $user will suffice; drop the quotes
# for this and all other instances

password => "$password",
version => 3 );

if (!$base) { $base = "ou=****,ou=*****,dc=******,dc=******"; }

if (!$attrs) { $attrs = [ 'memberOf' ]; }

$search = 'mail=*@**********.com';

$result = $ldap->search ( base => "$base",
scope => "sub",
filter => "$search",
attrs => $attrs
);


$href = $result->as_struct;

@arrayOfDNs = keys %$href;

foreach ( @arrayOfDNs ) {
$dn = $_;

# Above 2 lines could be reduced to:

foreach my $dn (@arrayOfDNs) {

$res = $ldap->compare ( $dn,
attr => "$attrs",
value => ''
);
@entries = $res->dn;


# Without thinking too hard about it, what does '->dn' mean in the above line: method call? hash dereference?

foreach $entr ( @enties ) {
$butes = $entr->dn;
print $butes, "\n";
}

print "#-------------------------------\n";
}

$mesg = $ldap->unbind;



This is pretty much a cut/paste of the example given in CPAN,

That will only be true once you've coded it more cleanly and rerun it with strictures and warnings.

jimk
.



Relevant Pages