Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 44 lines (36 sloc) 1.078 kB
3477ef1 @gbarr Add example script for recursive deletes
gbarr authored
1 #!/usr/bin/perl -w
2 #
3 # recursive-ldap-delete.pl
4 #
5 # Mike Jackson <mj@sci.fi>
6 #
7
8 use strict;
9 use Net::LDAP;
10
11 my $server = "localhost";
12 my $binddn = "cn=directory manager";
13 my $bindpasswd = "foobar";
14 my $base = "dc=bigcorp,dc=com";
15 my $delbranch = "ou=users,$base"; # branch to remove
16
17 my $ldap = Net::LDAP->new( $server ) or die "$@";
18 $ldap->bind( $binddn, password => $bindpasswd, version => 3 );
19 my $result = $ldap->search( base => $delbranch,
20 filter => "(objectclass=*)" );
21
22 my @dnlist;
23 my $entry;
24 foreach $entry ( $result->all_entries ) { push @dnlist, $entry->dn }
25
26 # explode dn into an array and push
27 # arrays to indexed hash of arrays
28 my %HoL;
29 my $i = 0;
30 for ( @dnlist ) {
31 s/,$base//;
32 $HoL{$i} = [ split(",", $_) ];
33 $i++;
34 }
35
36 # sorted descending by number of members (leaf nodes last)
37 foreach my $key ( sort { @{$HoL{$b}} <=> @{$HoL{$a}} } keys %HoL ) {
38 my $dn = join(",", @{ $HoL{$key} }).",$base";
39 $ldap->delete($dn);
40 }
41
42 $entry->update ( $ldap );
43 $ldap->unbind;
Something went wrong with that request. Please try again.