Search in a LDAP directory

This example connects to a LDAP server and makes a search for a name. The name was choosen by random (among those who returned an answer from the queried LDAP). The LDAP used in this example includes a binary certificate. To prevent this from trashing you terminal, it is not printed to the screen (binary field filtered in the attribute loop).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#!/usr/bin/perl -w
use strict;
 
use Net::LDAP;
my $ldap = Net::LDAP->new('directory.certifikat.dk') or die "$@";
$ldap->bind ;    # an anonymous bind
 
my $mesg = $ldap->search (  # perform a search
                       base   => "c=DK",
                       filter => "(&(cn=Henrik Jensen))"
 
                      );
 
$mesg->code && die $mesg->error;
 
print STDERR "Found " . $mesg->count . "n";
foreach my $entry ($mesg->all_entries) {
  my  @values = $entry->attributes();
  foreach my $key (@values) {
    print "$key => \"" . $entry->get_value($key) ."\"n" unless ($key =~ /binary/);
  }
}
 
exit();