-
Notifications
You must be signed in to change notification settings - Fork 1
/
ldap.pl
executable file
·52 lines (35 loc) · 1.09 KB
/
ldap.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#!/usr/bin/perl
use strict;
use Net::LDAP;
my $ldap_server = "mail.example.com";
my $BASEDN = "dc=example,dc=com";
my $user = 'amoore@example.com';
my $passwd = 'secret!';
my @fields = qw(cn mail sn givenname);
my $ldap = Net::LDAP->new($ldap_server) or die "$@";
#$ldap->bind;
my $mesg = $ldap->bind ( dn =>
$user,
password =>$passwd);
if ( $mesg->code()) {
die ("error:", $mesg->code(),"\n");
}
$ldap->debug( 0 );
my @result;
foreach my $ask_for (@ARGV) {
my $query = join '', map { "($_=$ask_for*) " } @fields;
my $mesg = $ldap->search(base => $BASEDN, filter => "(|$query)");
$mesg->code && die $mesg->error;
foreach my $entry ($mesg->all_entries) {
next unless ( $entry->get_value( 'mail' ) ); # can't mail them without an address
my $mail = $entry->get_value('mail');
my $name = $entry->get_value('givenName') . " " . $entry->get_value('sn');
my $telephone = $entry->get_value('telephoneNumber');
push(@result, "$mail\t$name\t$telephone");
}
}
print "LDAP query: found ", scalar(@result), "\n";
print join "\n", @result;
exit 1 if ! @result;
$ldap->unbind;
__END__