Created
July 12, 2011 20:11
-
-
Save fapestniegd/1078864 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/perl -w | |
| use strict; | |
| use Data::Dumper; | |
| use Net::LDAP; | |
| my $ldap = Net::LDAP->new( [ 'ldaps://faraday.eftdomain.net:636','ldaps://maxwell.eftdomain.net:636'] ) or die "$@"; | |
| my $mesg = $ldap->bind( 'uid=jameswhite,ou=People,dc=eftdomain,dc=net', | |
| password => $ENV{'WINDOWS_PASSWORD'}, | |
| ); | |
| # grab the entry we want to modify | |
| my $result = $ldap->search( # perform a search | |
| base => "ou=Operating Systems,ou=Sets,dc=eftdomain,dc=net", | |
| filter => "(cn=CentOS 5)", | |
| scope => "one", | |
| ); | |
| if($result->code){ | |
| print STDERR $result->error."\n"; | |
| exit; | |
| } | |
| # for each returned entry (should only be one, but we can loop over it anyway) | |
| foreach my $entry ($result->entries) { | |
| # get a list of our current members | |
| my @current_members = $entry->get_value('uniqueMember'); | |
| # clone it for modification | |
| my @new_members = @current_members; | |
| # loop through 1-39 (<40) | |
| for(my $i=1; $i<40; $i++){ | |
| # pad ant00 to ant39 and so on... | |
| for(my $pad=length($i); $pad < 2;$pad++){ $i="0$i"; } | |
| # push the new entry onto the list if it's not already in the list (push unless grep) | |
| push(@new_members,"cn=ant$i,ou=Hosts,dc=eftdomain,dc=net") | |
| unless grep(/^cn=ant$i,\s*ou=Hosts,\s*dc=eftdomain,\s*dc=net/,@new_members); | |
| } | |
| # replace the attribute in the entry with our new list | |
| $entry->replace('uniqueMember' => \@new_members); | |
| # dump it out to see what we changed | |
| # $entry->dump; | |
| # push the entry changes to the server | |
| $result = $entry->update($ldap); | |
| if($result->code){ | |
| print STDERR $result->error."\n"; | |
| exit; | |
| } | |
| } | |
| $ldap->unbind; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment