Listing 1. getempinfo.pl

#!/usr/bin/perl -w
use Net::LDAP;
use strict;

my $cn=$ARGV[0] || "none";
my $attr=$ARGV[1] || "none";

##: If nothing was given on command line then return
if($cn eq "none") {
  print STDERR "ERROR: No LDAP cn given\n";
  exit(1);
}

##: Bind anonymously to the ldap database
my $ldap=Net::LDAP->new('directory.domain.com',timeout=>5)
  or die "Couldn't connect to directory server.\n";
my $mesg=$ldap->bind('proxyuser@domain.com',password=>'proxyuser')
  or die "Couldn't connect to directory server.\n";

##: Query LDAP to get a list of employees
if($attr ne "none") {
  $mesg=$ldap->search( base=> "ou=Domain Users,dc=domain,dc=com",
                       filter=> "(sAMAccountName=$cn)",
                       attrs=> ['givenName','sn',"$attr"] );
} else {
  $mesg=$ldap->search( base=> "ou=Domain Users,dc=domain,dc=com",
                       filter=> "(sAMAccountName=$cn)",
                       attrs=> ['givenName','sn'] );
}

my $count=$mesg->count();
($count==1) or die "Error: LDAP enumeration error.";

my $entry=$mesg->entry();
my $value;
my @values;
if($attr ne "none") {
  $value="";
  @values=$entry->get_value("$attr");
  my $i=1;
  for(@values) {
    if($i>1) {
      $value.="/$_";
    } else {
      $value.=$_;
    }
    $i++;
  }
} else {
  $value=($entry->get_value('givenName')." ";
  $value.=$entry->get_value('sn'));
}

##: See if that attribute was defined for the given cn
if(!(defined($value))) {
  print STDERR "ERROR: That attribute was not defined.\n";
  exit(1);
}

$mesg=$ldap->unbind;
print("$value\n");