For Programmers: Free Programming Magazines  


Home > Archive > LDAP > March 2006 > LDAP search question









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author LDAP search question
TwistFactory

2006-03-16, 9:57 pm

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=3Dlam and my username is cn=3Droot,dc=3Dlam=
..
What am I doing wrong to grab a set of entries similar to
cn=3Dhm-6YOC8298,ou=3Dhomes,dc=3Dlam ? Thanks a lot!



#!/usr/bin/perl

use Net::LDAP;

my $ad =3D Net::LDAP->new("authdev") or die "$0";

my $err =3D $ad->bind(dn=3D>"cn=3Droot,dc=3Dlam", password=3D>"xxx") . "\n"=
; #
$err prints "Net::LDAP::Bind=3DHASH(0x807a16c)"

my $searchbase =3D 'ou=3Dhomes,dc=3Dlam';

my $filter =3D "(cn=3D*)";

my $attrs =3D "mail";

my $results =3D $ad->search(base=3D>$searchbase,filter=3D>$filter,attrs=3D>=
$attrs);

my $count =3D $results->count;

print "Total entries returned: $count\n";

$ad->unbind;
Chris Ridd

2006-03-17, 3:58 am

On 16/3/06 6:56, TwistFactory <twistfactory@gmail.com> 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


Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com