I love Free Software!

Pages

Funny Quotes to Think

When you say "I wrote a program that crashed Windows", people just stare at You blankly and say "Hey, I got those with the system, for free"

-Torvalds, Linus(1995-03-08)-

Monday, December 7, 2009

Perl script for adding a LDAP group members to another group

This script will help you to add an LDAP group to another group.
  •  Script will accept two arguments. Parent group and child group (Group which you need to be merged)
  • Script will take each member from child group and add to parent group
  • If member is already exists, script will skip adding member
  • If member does not exists, this will add attributes `member` and `memberUid`

#!/usr/bin/perl
# This script is used to merge members of one group to another.
# Script need two arguments parent and child groups. All members from child group will be -
# added to Parent group. Script skip adding if member already exist.
# Created by Praveen C (praveen.velu84@yahoo.com)

use Net::LDAP;

$ldap = Net::LDAP->new("localhost");
$ldap->bind("cn=admin,dc=example,dc=com", password=>"secret");

# Get Parent Group Email-ID here

print "Enter Email id of parent group:";
$parentid = <STDIN>
chomp $parentid;

# Get Child group Email-ID(Group need to be merged)

print "Enter Email id of child group:";
$childid = <STDIN>
chomp $childid;

# Search for Both Parent and Child groups in ldap

$parent = $ldap->search(filter=>"(&(mail=$parentid)(objectclass=posixGroup))"
, base=>"dc=example,dc=com");
$child = $ldap->search(filter=>"(&(mail=$childid)(objectclass=posixGroup))"
, base=>"dc=example,dc=com");
$pentry = $parent->entry(0);
$centry = $child->entry(0);

# Assign `maildrop` of child group in to array
@cemails = $centry->get( 'maildrop' );

# Get count of members in child group
my $max = scalar @cemails;
print "$max Members found in Child Group\n\n";
print "Parent DN: " . $pentry->dn(). "\n";
print "Child DN: " . $centry->dn(). "\n";
for( my $index = 0 ; $index < $max ; $index++){ $r = $ldap->compare( $pentry->dn(),attr => 'member',value => @cemails[$index]);
if($r->code == 5){
$getuid = $ldap->search(filter=>"(&(mail= @cemails[$index])(objectclass=posixAccount))"
, base=>"dc=example,dc=com");
$uentry = $getuid->entry(0);
@myuid = $uentry->get( 'uid' );
$msg = $ldap->modify($pentry->dn(),add=>{'member' => @cemails[$index]});
$msg = $ldap->modify($pentry->dn(),add=>{'memberUid' => @myuid[0]});
print "Adding @cemails[$index] with memberuid @myuid[0]\n";
}
else{
print "Member @cemails[$index] already exists\n";
}
}

No comments:

Post a Comment