in reply to Phone lookup on Exchange server

I started on something for CPAN that I never got around to finishing. I started this because I wanted to create a mutt alias file that contained everyone in the company by accessing Exchange via LDAP. Here are the basics.

A sample calling program:

#!/usr/bin/perl -w use strict; use Net::LDAP::Exchange; use Data::Dumper; use constant SERVER => 'Exchange Server Here'; use constant DOMAIN => 'Domain Here'; # If you need to login... use constant LOGIN => 'Exchange Login Here'; use constant PASSWORD => 'Password Here'; my $ex = Net::LDAP::Exchange->new(SERVER,DOMAIN,LOGIN,PASSWORD); my $result_set = $ex->search_lastname('Bisbee'); print Dumper($result_set);
 

Here is the skelton of my module to search by last name:

package Net::LDAP::Exchange; use strict; use Net::LDAP; use Data::Dumper; sub new { my ($class, $server, $domain, $name, $password) = @_; die 'server and domain are required' unless $server and $domain; die 'missing password' if $name and !$password; die 'missing name' if $password and !$name; my $self = bless {}, $class; $self->{ldap} = Net::LDAP->new($server); $self->{server} = $server; $self->{domain} = $domain; $self->{name} = $name if $name; $self->{password} = $password if $password; return $self; } sub search_lastname { my ($self,$search) = @_; my $query = _build_query($search,qw(CN)); my $result_set = $self->search($query); return $result_set; } sub search { my ($self,$query) = @_; if ($self->{name} and $self->{password}) { $self->{ldap}->bind('cn='.$self->{name}, password=>$self->{password}, version=>3); } else { $self->{ldap}->bind; } my $results = $self->{ldap}->search(base => 'o=' . $self->{domain}, filter => "(|$query)"); $results->code && die $results->error; my @result_set = (); foreach my $entry ($results->all_entries) { my @attributes = $entry->attributes; my $result = {}; for my $att (@attributes) { my $return_att = uc $att; $result->{$return_att} = $entry->get_value($att); } push @result_set, $result; } $self->{ldap}->unbind; return \@result_set; } sub _build_query { my ($search,@fields) = @_; my $query = join '', map { "($_=$search*) " } @fields; return $query; }
If you have any comments or suggestions, send them to jbisbee at yahoo dot com.

-biz-

Replies are listed 'Best First'.
Re: Re: Phone lookup on Exchange server
by jlf (Scribe) on Feb 13, 2002 at 17:35 UTC
    Biz,

    I see that your module searches by surname -- but how did you obtain your exhaustive list of Exchange users? (Or did you?) I had to resort to Win32::NetAdmin for that but it seems like there should be some LDAP query that accomplishes the same result.

    Josh

      There is a max limit you can query on with LDAP (at least the exchange server that I played with did). I just queried on "A" through "Z" and figured that would work. Test this against your current list and make sure you get everyone.
      sub get_company_list { my ($self) = @_; my @company_list = (); for my $search ("A".."Z") { push @company_list, @{$self->search_lastname($search)}; } return \@company_list; }
      -biz-