0

我没有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;
        }
    ]

);

我不知道他们之间有什么更好的。但他们工作。

实际上,如果旧密码错误,我的脚本也可以正常工作。我想在脚本继续之前控制旧密码,或者如果旧密码错误,脚本可能会重新启动。我想了一个循环,我试着放的是展台脚本没有成功。我可以帮忙吗?

4

1 回答 1

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();
于 2013-03-13T12:58:58.440 回答