in reply to LDAP address lookup from Mutt

This is my first comment...

I grabbed this script to give me ldap access to my server, naturally. However my mail attribute is multivalued for a number of entries, so I have made some modifications to the script to handle the multivalued attribute.

Make sure you change all entries as required.

#!/usr/bin/perl use strict; use Net::LDAP; use constant HOST => 'ldap.your.domain'; use constant BASE => 'ou=People, dc=your, dc=domain'; use constant VERSION => 3; use constant SCOPE => 'sub'; my $name; my @attributes = qw( dn givenName sn mail ); my $filter = "(|(sn=$name*)(givenName=$name*))"; { print "Searching directory... "; $name = shift || die "Usage: $0 filter\n"; my $ldap = Net::LDAP->new( HOST, onerror => 'die' ) || die "Cannot connect: $@"; $ldap->bind(version => VERSION) or die "Cannot bind: $@"; my $result = $ldap->search( base => BASE, scope => SCOPE, attrs => \@attributes, filter => $filter ); my @entries = $result->entries; $ldap->unbind(); print scalar @entries, " entries found.\n"; foreach my $entry ( @entries ) { my @emailAddr = $entry->get_value('mail'); foreach my $addr (@emailAddr) { print $addr , "\t"; print $entry->get_value('givenName'), " "; print $entry->get_value('sn'), "\n"; } } }

Make sure you have the Net::LDAP module installed.

Hope someone finds it of use, and thanks to the original author... I just hacked on what was already provided.

Replies are listed 'Best First'.
Re: Re: LDAP address lookup from Mutt
by waxhead (Acolyte) on Dec 26, 2002 at 13:33 UTC

    There's a slight error... sorry about that...

    Move the line creating the filter after the line that shift's the parameter into $name:

    $name = shift || die "Usage: $0 filter\n"; my $filter = "(|(sn=$name*)(givenName=$name*))";

    This works much better.. :-/

      After searching endlessly for a straight forward ldap search script sample that actually returned info I can use immediately and not have to through into an array to further massage(because I don't understand the Net::LDAP interworkings), This is is awesome! This should be the sample script used to teach folks who don't yet understand the Perl OO stuff and would like to use Net::LDAP. Some minor issues with "Odd number of elements in anonymous hash", but otherwise works great and was able to further modify to use. Frankly, the official examples(as someone new to using the Net::LDAP" are not very good as far as the search goes. This example is just fantastic, now I can start digging further for better understand this. But honestly, I don't know why some of these modules are built so complex to use. At any rate, Thanks Lachoy!!