1: This code searches an ldap server and returns records
   2: without an email address in a particular branch of the 
   3: database (here, specified by the 'ou' attribute).
   4: 
   5: 
   6: #!/usr/local/bin/perl -w
   7: 
   8: use strict;
   9: use Net::LDAP;
  10: use Text::CSV_XS;
  11: 
  12: my $outfile = "noemail.csv";
  13: my $host = "yourldapserver.com";
  14: my $port = "389";
  15: my $ldap = Net::LDAP->new($host, port=>$port, timeout=>10)or die "Can not create new LDAP object: $!\n";
  16: my $bind_result = $ldap->bind() or die "Could not bind to $host: $!\n";
  17: # $searchbase varies (perhaps significantly)
  18: # depending on how you built you ldap server
  19: my $searchbase = "o=yourcompany,c=yourcountry";
  20: my $organization = "yourorg";
  21: my $csv = Text::CSV_XS->new();
  22: 
  23: open(UNI,">$outfile");
  24: 
  25: my $filt = "(&(!(mail=*))(ou=$organization))";
  26: ldapsearch($filt);
  27: 
  28: 
  29: sub ldapsearch{
  30:     my $filter = shift;
  31:     my $search = $ldap->search(
  32:                         base=>$searchbase,
  33:                         filter=>$filter,
  34:                         callback=>\&callback,
  35:                         );
  36: }
  37: 
  38: 
  39: sub callback {
  40:     # Use callback if you search a lot
  41:     # of records and want some kind of feedback
  42:     # as each returned record is processed.
  43:     my ($search,$entry) = @_;
  44:     my $number = $search->count;
  45:     $entry = $search->shift_entry;
  46:     my @results=();
  47: 
  48:     if($entry){
  49:         # Our $entry always contains
  50:         # dn, cn, givenname, surname, and alias.
  51:         # Manager and tele may or may not exist.
  52:         my $results[0] = $entry->dn;
  53:         my $results[1] = $entry->get_value('cn');
  54:         my $results[2] = $entry->get_value('givenname');
  55:         my $results[3] = $entry->get_value('surname');
  56:         my $results[4] = $entry->get_value('alias');
  57:         my $results[5] = $entry->get_value('manager') || "No Manager Listed";
  58:         my $results[6] = $entry->get_value('telephonenumber') || "No Phone Listed";
  59:            
  60:         my $status = $csv->combine(@results);
  61:         my $line = $csv->string();
  62:         print UNI $line."\n";
  63: #        print UNI "\"$dn\",$given,$sn,$alias,$tele,\"$manager\"";
  64:         print "$cn\n";
  65:     }
  66: }
  67: 
  68: close(UNI);
  69: 
  70: 
  71: 
  72: Updated (10/24) - Taking a tip from someone
  73: more learned in the ways of PM,
  74: my blatant plea for first post mercy is 
  75: now removed. =)
  76: 
  77: Updated (10/26) - '-' at line 59 is now '='.
  78: Thanks, Fokat.
  79: 
  80: Updated (12/21) - Now checking $bind_result and using
  81: Text::CSV for output data. Removed 'To do' comments as a
  82: result. Fixed $searchbase and $filt so that results would 
  83: actually be returned. Before, $searchbase was restrictive
  84: enough that the filter would have no branch of ldap to 
  85: search.

Replies are listed 'Best First'.
Re: LDAP search with callback
by fokat (Deacon) on Oct 25, 2001 at 22:55 UTC
    Line 59 should be...
    59: my $manager = $entry->get_value('manager') || "No Manag +er Listed";
    (Note the equal sign).