#!/usr/bin/perl use Net::LDAP; @fields = # fields to export ( # Label Field Name Index ['UID','uid',0], ['mail','mail',0], ['OtherPhone','OtherPhone',0], ['mobile','mobile',0], ['givenName','givenName',0], ['Initials','initials',0], ['sn','sn',0], ['c','c',0], ); $ldap = Net::LDAP->new('hostname:port') or die "$@"; # open connection $mesg = $ldap->bind( # login "cn=Directory Manager", password => "$$$$$$$$$$$"); $mesg = $ldap->search( # search base => "dc=xyz,dc=com", filter => "(&(objectClass=*)(!(c=US))(uid=*))" ); $DATETIME=`date`; sub get # get field value { return $_[0]->exists($_[1]) ? $_[0]->get($_[1])->[$_[2] ? $_[2] : 0] : ''; } print "$DATETIME"; # first line with file created date/time print '"'.(join '","', "Report", # header map {$_->[0]} @fields)."\"\n"; foreach $entry ($mesg->all_entries) # for each entry { @dn = map {s/[a-z]+=//gi; $_ = ucfirst} # organization reverse split /,/, $entry->dn; shift @dn; shift @dn; shift @dn; pop @dn; print '"'.(join '","', "@dn", # row map {get $entry, $_->[1], $_->[2]} @fields)."\"\n"; } $mesg = $ldap->unbind; # close connection