Skip to content

Instantly share code, notes, and snippets.

@pkarman
Created June 2, 2015 14:32
Show Gist options
  • Select an option

  • Save pkarman/1143a496b78ceec0f925 to your computer and use it in GitHub Desktop.

Select an option

Save pkarman/1143a496b78ceec0f925 to your computer and use it in GitHub Desktop.
```perl
#!/usr/bin/env perl
use strict;
use Data::Dump qw( dump );
use Net::LDAP::Class::User::AD;
use Net::LDAP::Class::Group::AD;
use Path::Class;
my $usage = "$0 first-or-last-name\n";
my $name = shift(@ARGV) or die $usage;
chomp( my $user_name = `whoami` );
chomp( my $proper_name = file("$ENV{HOME}/.w/.user")->slurp );
chomp( my $old_pass = file("$ENV{HOME}/.w/.pass")->slurp );
if ( !$proper_name or !$old_pass ) {
die "Can't find credentials in $ENV{HOME}/.w/";
}
my $BASE_DN = 'DC=mpr,DC=org';
my $AD_IP = 'ldap://mpr.org:389';
my $ldap = Net::LDAP->new($AD_IP);
$ldap or die "can't connect to LDAP: $!";
# mangle the CN value
my $cn = $proper_name;
$cn =~ s/^(.+)\ +(.+)$/$2\\, $1/;
my $mesg = $ldap->bind(
dn => "CN=$cn,OU=Minnesota,OU=People,$BASE_DN",
password => $old_pass,
) or die "can't bind";
$mesg->code && die $mesg->error;
{
package UserClass;
@UserClass::ISA = qw( Net::LDAP::Class::User::AD );
UserClass->metadata->setup(
ldap => $ldap,
base_dn => $BASE_DN,
attributes =>
[ @{ __PACKAGE__->AD_attributes }, qw( telephoneNumber mobile otherMobile otherTelephone ) ],
unique_attributes => __PACKAGE__->AD_unique_attributes,
);
sub init_group_class {'GroupClass'}
}
{
package GroupClass;
@GroupClass::ISA = qw( Net::LDAP::Class::Group::AD );
GroupClass->metadata->setup(
ldap => $ldap,
base_dn => $BASE_DN,
attributes => __PACKAGE__->AD_attributes,
unique_attributes => __PACKAGE__->AD_unique_attributes,
);
sub init_user_class {'UserClass'}
}
my %find_args = (
ldap => $ldap,
base_dn => $BASE_DN,
filter => "(&(displayName=*${name}*))"
);
#dump \%find_args;
my $users = UserClass->find(%find_args);
if ( !$users or !@$users ) {
print "No Users found for $name\n";
exit(0);
}
for my $user (@$users) {
for my $attr (qw( displayName mail telephoneNumber )) {
printf( "%15s : %s\n", $attr, $user->$attr );
}
print '-' x 70, $/;
#dump $user->read()->ldap_entry->dump;
}
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment