Category: | Utility Scripts |
Author/Contact Info | mayaTheCat |
Description: | hi monks,
the following code recursively deletes a subtree from LDAP;
if the port is other than the default port (389), it can be appended at the back of the server string, delimited with a ':';
if we do not want to stress the server, we can periodically pause the deletion for a while through the parameters $sleepPeriod and sleepDuration |
sub ldapRecursiveDelete {
my ($server, $subtreeDN, $user, $password, $sleepPeriod, $sleepDur
+ation) = @_;
my ($ldap, @toBeSearched, @toBeDeleted);
return unless defined $subtreeDN;
$sleepPeriod = 2000 unless defined $sleepPeriod;
$sleepDuration = 1 unless defined $sleepDuration;
use Net::LDAP;
$ldap = Net::LDAP->new($server);
if (defined $user && defined $password) {
$ldap->bind($user, password => $password);
} else {
$ldap->bind;
}
@toBeSearched = ( $subtreeDN );
while (@toBeSearched) {
$_ = shift @toBeSearched;
push @toBeDeleted, $_;
for (
(
$ldap->search(
base => $_
, scope => 'one'
, filter => '(objectclass=*)'
, attrs => [ '1.1' ]
)
)->entries
) {
push @toBeSearched, $_->dn;
}
}
my $i = 0;
while (@toBeDeleted) {
$ldap->delete(pop @toBeDeleted);
sleep $sleepDuration unless ++$i % $sleepPeriod;
}
$ldap->unbind;
}
|
|
---|