我没有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;
}
]
);
我不知道他们之间有什么更好的。但他们工作。
实际上,如果旧密码错误,我的脚本也可以正常工作。我想在脚本继续之前控制旧密码,或者如果旧密码错误,脚本可能会重新启动。我想了一个循环,我试着放的是展台脚本没有成功。我可以帮忙吗?