我在perl中没有超级技能。但是,我做了这两个用户更改LDAP密码的脚本。
第一个脚本:
#!/usr/bin/perl -w
use strict;
use Expect;
my $user= getpwuid( $< );
print "Enter your old password :" ;
my $oldpassword = <STDIN>;
chomp($oldpassword);
print "Enter you new password :";
my $newpassword = <STDIN>;
chomp($newpassword);
print "Running ' passwd ${user}'\n";
my $exp = Expect->spawn("passwd") or die "Can t acces to passwd \n";
unless ($exp->expect(1, "Enter login\(LDAP\) password:")) {} ;
print $exp "${oldpassword}\r" ;
unless ($exp->expect(1, "New password:")) {} ;
print $exp "${newpassword}\r" ;
unless ($exp->expect(1, "Re-enter new password:")) {} ;
print $exp "${newpassword}\r" ;
$exp->soft_close();
第二个脚本:
#!/usr/bin/perl -w
use strict;
use Expect;
my $user= getpwuid( $< );
print "Enter your old password :" ;
my $oldpassword = <STDIN>;
chomp($oldpassword);
print "Enter your new password :";
my $newpassword = <STDIN>;
chomp($newpassword);
print "Running ' passwd ${user}'\n";
my $spawn_ok;
my $exp = Expect->spawn("passwd") or die "Can t acces to passwd \n";
$exp->expect(1,
[qr 'Enter login\(LDAP\) password:' ,
sub {
$spawn_ok = 1;
my $fh = shift;
$fh->send("${oldpassword}\n");
print "sent '${oldpassword}'\n";
exp_continue;
}
],
[eof =>
sub {
if ($spawn_ok) {
die "ERROR: premature EOF in login.\n";
} else {
die "ERROR: could not spawn old password.\n";
}
}
],
['New password: ' ,
sub {
my $fh =shift ;
$fh->send("${newpassword}\n");
print "sent '${newpassword}'\n";
exp_continue;
}
],
['Re-enter new password:' ,
sub {
my $fh =shift ;
$fh->send("${newpassword}\n");
print "sent '${newpassword}'\n";
exp_continue;
}
]
);
我不知道他们之间有什么关系。但他们工作。
实际上,如果旧密码错误,我的脚本工作正常。我希望在脚本继续之前控制旧密码,或者如果旧密码错误,脚本可能会重新启动。我想到了一个循环,我试图把展位脚本没有成功。 我可以帮忙吗?
答案 0 :(得分:0)
你知道Net :: Ldap模块吗?
http://search.cpan.org/~gbarr/perl-ldap/lib/Net/LDAP/FAQ.pod#Ho_do_I_reset_a_user%27s_password_ ...
没有错误检查!
use Net::LDAP;
my $ldap = Net::LDAP->new('ldaps://server.domain') or die "$@";
my $mesg = $ldap->bind('cn=Joe User,dc=perl,dc=ldap,dc=org',
password => 'oldPW');
my $rootdse = $ldap->root_dse();
if ($rootdse->supported_extension('1.3.6.1.4.1.4203.1.11.1') {
require Net::LDAP::Extension::SetPassword;
$mesg = $ldap->set_password(user => 'cn=Joe User,dc=perl,dc=ldap,dc=org',
oldpasswd => 'oldPW',
newpasswd => 'newPW');
}
else {
$mesg = $ldap->modify('cn=Joe User,dc=perl,dc=ldap,dc=org',
changes => [
delete => [ userPassword => $oldPW ]
add => [ userPassword => $newPW ] ]);
}
$ldap->unbind();