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