On 16/3/06 6:56, TwistFactory <[EMAIL PROTECTED]> wrote:

> Hi guys,
> 
> I'd like to start automating a few things on my LDAP server with Perl
> but I haven't been able to successfully log in and search yet. In LDAP
> Administrator, my base DN is dc=lam and my username is cn=root,dc=lam.
> What am I doing wrong to grab a set of entries similar to
> cn=hm-6YOC8298,ou=homes,dc=lam ? Thanks a lot!
> 
> 
> 
> #!/usr/bin/perl
> 
> use Net::LDAP;
> 
> my $ad = Net::LDAP->new("authdev") or die "$0";
> 
> my $err = $ad->bind(dn=>"cn=root,dc=lam", password=>"xxx") . "\n"; #
> $err prints "Net::LDAP::Bind=HASH(0x807a16c)"

Methods that send ops to the directory don't return error codes, they return
undef (if something really horrible happened locally) or an object. You have
to query the object to find out what error the directory sent you.

The returned object is a Net::LDAP::Message, and the documentation for that
class shows you useful things you can call on it, like code() to get the
result code.

Also you don't pass the bind name using 'dn => "cn=root,dc=lam", you just
pass it as '"cn=root,dc=lam"'.

Read the Net::LDAP documentation!

> my $searchbase = 'ou=homes,dc=lam';
> 
> my $filter = "(cn=*)";
> 
> my $attrs = "mail";
> 
> my $results = $ad->search(base=>$searchbase,filter=>$filter,attrs=>$attrs);

The search method returns an object too, but this time you're handling it
correctly. This time though, the object's from a subclass of
Net::LDAP::Message, namely Net::LDAP::Search. It has a few extra methods to
let you get at the returned entries.

> my $count = $results->count;
> 
> print "Total entries returned: $count\n";
> 
> $ad->unbind;

Cheers,

Chris


Reply via email to