1. Install Perl-LDAP
: Http://search.cpan.org /~ Gbarr/perl-ldap/
The premise is that you have installed the Perl toolkit. First, check the Perl version.
[Root @ local ~] Perl-V
Built under Linux
Compiled at Nov 8 2007 06:49:06
@ INC:
/Usr/lib64/perl5/site_perl/5.8.8/x86_64-linux-thread-multi
/Usr/lib/perl5/site_perl/5.8.8
/Usr/lib/perl5/site_perl
/Usr/lib64/perl5/vendor_perl/5.8.8/x86_64-linux-thread-multi
/Usr/lib/perl5/vendor_perl/5.8.8
/Usr/lib/perl5/vendor_perl
/Usr/lib64/perl5/5.8.8/x86_64-linux-thread-multi
/Usr/lib/perl5/5.8.8
Next, unzip the package and install it (the installation method is manual and you do not need to execute make)
[Root @ local ~] Tar-zxvpf perl-ldap-0.39.tar.gz
[Root @ local ~] CD perl-ldap-0.39/lib
[Root @ local ~] CP-A */usr/lib/perl5/site_perl/5.8.8
2. Install the convert: ASN1 module on which Perl-LDAP depends
: Http://search.cpan.org/search? Module = convert: ASN1
[Root @ local ~] Tar-zxvpf Convert-ASN1-0.22.tar.gz
[Root @ local ~] CD Convert-ASN1-0.22
[Root @ local ~] Perl makefile. pl
[Root @ local ~] Make
[Root @ local ~] Make install
3. Use Perl-LDAP to modify a user's password in MS Active Directory
[Root @ local ~] $ Vim chg_passwd.pl
#! /Usr/bin/perl-W
Use strict;
Use Net: LDAPS;
My ($ AD, $ mesg, $ uid, $ pass, $ npass, $ DN, $ RTN );
# ($ Uid, $ pass) = Split ("", <stdin> );
$ Uid = "test ";
$ Pass = "123456 ";
If ($ uid EQ "") or ($ pass EQ "")){
Print "UID and/or password missing in input/N"; Exit 1;
}
Print "trying to set $ UID to password $ pass/N ";
#1. bind to the ad server
$ Ad = net: LDAPS-> New ("ad02.example.com", Port => 636, version => 3) or print "unable to connect to Ad server/N ", exit 2;
$ Ad-> BIND (DN => "cn = administrator, ou = finance, Dc = example, Dc = com", password => "123456 ") or print "unable to bind to Ad server/N", Exit 2;
#2. Do a ad lookup to get the DN for this user
$ Mesg = $ ad-> Search (base => "DC = example, Dc = com", filter => "cn = $ uid ");
Print $ mesg-> count;
Print "/N ";
If ($ mesg-> count! = 1 ){
Print "ad lookup failed for user $ UID/N"; Exit 3;
}
#4. Add quotes and Unicode
Map {$ npass. = "$ _/000"} split (//, "/" $ pass /"");
#5. Now change their password.
$ DN = $ mesg-> entry (0)-> dn;
$ RTN = $ ad-> modify ($ DN, replace => ["unicodepwd" => $ npass]);
If ($ RTN-> {'result '}! = 0 ){
Print "User $ uid, Setting Password failed/N"; Exit 2;
}
#6. Free
$ Ad-> unbind ();
Print "password for $ uid changed in AD/N ";
Exit 0;