如果以下脚本可以批量修改服务器的密码,并且导出随机生成的密码
使用方法:
查看新生成的密码文件
首先需要安装Net::SSH::Expect
安装方法:
#cpan
cpan>install
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[int rand @array_list]}0..7;#生成随机密码 0..7表示8位密码
@ssh_list=split/\s+/,$_;
print $ssh_list[0]."正在修改密码\n";
&ssh_test( "$ssh_list[0]", "$ssh_list[1]", "$ssh_list[2]", "$ssh_list[3]" );
}
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[3]");
$ssh->send("su - root"); # 其实这里我本来的用户不是root,为了更好的扩展
$ssh->waitfor( 'Password:\s*$', 2 );# 经常服务器不容许root直接登录的,所以要用其他
$ssh->send("$ssh_list[3]"); #用户来切换root
$ssh->waitfor( '#\s*', 2 );
$ssh->send("passwd $ssh_list[2]");
$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";
}
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[int rand @array_list]}0..7;#生成随机密码 0..7表示8位密码
@ssh_list=split/\s+/,$_;
print $ssh_list[0]."正在修改密码\n";
&ssh_test( "$ssh_list[0]", "$ssh_list[1]", "$ssh_list[2]", "$ssh_list[3]" );
}
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[3]");
$ssh->send("su - root"); # 其实这里我本来的用户不是root,为了更好的扩展
$ssh->waitfor( 'Password:\s*$', 2 );# 经常服务器不容许root直接登录的,所以要用其他
$ssh->send("$ssh_list[3]"); #用户来切换root
$ssh->waitfor( '#\s*', 2 );
$ssh->send("passwd $ssh_list[2]");
$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";
}
[root@mysqlmaster ~]# cat oldpasswd.txt
192.168.1.91 22 root fmnuy5ci
192.168.1.95 22 root bortv8bb
[root@mysqlmaster ~]# ./sshpwd.pl oldpasswd.txt
192.168.1.91正在修改密码
修改完成
------------------------------
192.168.1.95正在修改密码
修改完成
------------------------------
192.168.1.91 22 root fmnuy5ci
192.168.1.95 22 root bortv8bb
[root@mysqlmaster ~]# ./sshpwd.pl oldpasswd.txt
192.168.1.91正在修改密码
修改完成
------------------------------
192.168.1.95正在修改密码
修改完成
------------------------------
[root@mysqlmaster ~]# cat /home/mcshell/newuser.txt
--------------------------------------------------------------------------------
192.168.1.91 22 root 9f8mu1gs
--------------------------------------------------------------------------------
192.168.1.95 22 root 1p2p2y0y
--------------------------------------------------------------------------------
192.168.1.91 22 root 9f8mu1gs
--------------------------------------------------------------------------------
192.168.1.95 22 root 1p2p2y0y
本文转自 mcshell 51CTO博客,原文链接:http://blog.51cto.com/mcshell/929109,如需转载请自行联系原作者