perl 利用Net::SSH::Expect模块批量修改服务器密码
#!/usr/bin/perl use strict;use warnings;
use Net::SSH::Expect;
my @ssh_list;
my @array_list=(0..9,'a'..'z');# 生成随机种子
my $new_user_pass;
while () {
$new_user_pass=join '', map{$array_list}0..7;#生成随机密码 0..7表示8位密码
@ssh_list=split/\s+/,$_;
print $ssh_list."正在修改密码\n";
&ssh_test( "$ssh_list", "$ssh_list", "$ssh_list", "$ssh_list" );
}
sub ssh_test() {
my ( $host, $port, $user, $pass ) = @_;
my $ssh = Net::SSH::Expect->new(
host => $host,
port => $port,
password => $pass,
user => $user,
no_terminal => 0,
raw_pty => 1,
timeout => 6,
);
open FI, ">> /home/mcshell/newuser.txt" or die $!;##新密码所放的位置
print FI "-" x 80, "\n";
$ssh->debug(0);
$ssh->run_ssh() or die "SSH process couldn't start: $!";
$ssh->waitfor( '\(yes\/no\)\?$', 2 ); #交互式修改密码,给予2秒的时间
$ssh->send("yes\n");
$ssh->waitfor( 'password:\s*$/', 2);
$ssh->send("$ssh_list");
$ssh->send("su - root"); # 其实这里我本来的用户不是root,为了更好的扩展
$ssh->waitfor( 'Password:\s*$', 2 );# 经常服务器不容许root直接登录的,所以要用其他
$ssh->send("$ssh_list"); #用户来切换root
$ssh->waitfor( '#\s*', 2 );
$ssh->send("passwd $ssh_list");
$ssh->waitfor( 'password:\s*$', 2 );
$ssh->send("$new_user_pass");
$ssh->waitfor( 'password:\s*$', 2 );
$ssh->send("$new_user_pass");
$ssh->waitfor( '#\s*', 2 );
print FI "$host\t$port\t$user\t$new_user_pass\n";
$ssh->close();
close FI;
print "修改完成\n";
print "-" x 30, "\n";
}
页:
[1]