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).

#!/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();