Linux: LDAP automation

From ReceptiveIT
Jump to: navigation, search

Get User Script

userget.pl

#!/usr/bin/perl

use Net::LDAP;
use strict;

my $ldap = Net::LDAP->new('192.168.0.10') or die "[email protected]";
my $mesg = $ldap->bind('cn=admin,dc=bigdomain,dc=local', password => '<password here>');   
my $userid;
my $users_dn = "ou=users,dc=bigdomain,dc=local";

while(<>) {
  chomp($_);
  my $prefix = $_;
  my $search_dn;

  if( $prefix ) {
    if( substr($prefix,0,3) eq "ou=") {
      $search_dn = $prefix . "," . $users_dn;
    } else {
      $search_dn = "ou=" . $prefix . "," . $users_dn;
    }
  } else {
    $search_dn = $users_dn;
  }

  #print "Search dn: $search_dn\n";
  #print "-------------------------------------------------\n";
  #print "Searching LDAP - ";

  $mesg = $ldap->search(base => $search_dn, filter  => "(uid=*)");

  if( ! $mesg->is_error() ) {
    #print("OK\n");
  } else {
    print("ERROR\n");
  }

  my $max = $mesg->count;

  #print "Matching records - $max\n";

  my $first;
  my $last;
  my $username;
  my $entry;

  for( my $index = 0 ; $index < $max ; $index++) {
    $entry = $mesg->entry($index);
    $first = $entry->get_value( 'givenName' );
    $last = $entry->get_value( 'sn' );
    $username = $entry->get_value( 'uid' );

    #print "First Name: $first\n";
    #print "Surname: $last\n";
    #print "Username: $username\n";
    print "$username\n";

    #print "-------------------------------------------------\n";
  }

  if( ! $mesg->is_error() ) {
    #print("OK\n");
  } else {
    print("ERROR\n");
  }

  if( $mesg->code > 0 ) {
    $mesg = $ldap->unbind;   # take down session
    exit();
  }
}

$mesg = $ldap->unbind;   # take down session

exit();

Email Replace

emailreplace.pl

#!/usr/bin/perl

use Net::LDAP;
use strict;

my $ldap = Net::LDAP->new('127.0.0.1') or die "[email protected]";
my $mesg = $ldap->bind('cn=admin,dc=bigdomain,dc=local', password => '<password here>');   
my $userid;
my $users_dn = "ou=users,dc=bigdomain,dc=local";

while(<>) {
  chomp($_);
  my $username = $_;

  print "Username: $username\n";
  print "Search dn: $users_dn\n";
  print "-------------------------------------------------\n";
  print "Searching LDAP - ";

  $mesg = $ldap->search(base => $users_dn, filter  => "(uid=$username)");

  if( ! $mesg->is_error() ) {
    print("OK\n");
  } else {
    print("ERROR\n");
  }

  my $max = $mesg->count;

  print "Matching records - $max\n";

  if( $max == 1 ) {
    my $first;
    my $last;

    for( my $index = 0 ; $index < $max ; $index++) {
      my $entry = $mesg->entry($index);
      $first = $entry->get_value( 'givenName' );
      $last = $entry->get_value( 'sn' );

      print "First Name: $first\n";
      print "Surname: $last\n";
      print "-------------------------------------------------\n";
      print "Replacing email information...";

      $entry->delete( mailLocalAddress => [] );
      $entry->add( mailLocalAddress => $username . "\@bigdomain.com.au" );
      $entry->add( mailLocalAddress => $username . "\@otherbigdomain.com.au" );
      $entry->delete( mailRoutingAddress => [] );
      $entry->add( mailRoutingAddress => $username . "\@bigdomain" );
      $entry->delete( mailHost => [] );
      $entry->add( mailHost => "192.168.0.10" );

      $mesg = $entry->update($ldap);

      if( ! $mesg->is_error() ) {
        print("OK\n");
      } else {
        print("ERROR\n");
      }
    }
  } else {
    print "There should only be one match... Aborting.\n";
  } 

  print "-------------------------------------------------\n";

  if( $mesg->code > 0 ) {
    $mesg = $ldap->unbind;   # take down session
    exit();
  }
}

$mesg = $ldap->unbind;   # take down session

exit();