如果以下脚本可以批量修改服务器的密码,并且导出随机生成的密码
使用方法:
首先需要安装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 /home/mcshell/newuser.txt -------------------------------------------------------------------------------- 192.168.1.91 22 root 9f8mu1gs -------------------------------------------------------------------------------- 192.168.1.95 22 root 1p2p2y0y
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。