perl批量更改服务器密码
2010-03-17 15:47
218 查看
#!/usr/bin/perl
use strict;
use warnings;
use Net::SSH::Expect;
use String::MkPasswd qw(mkpasswd);
my $user = "user";
my $pass = 123456;
my $root_pass = 123456;
my $new_user_pass = mkpasswd(-length => 15, -minnum => 3, -minlower => 3 , -minupper => 3, -minspecial => 3);
my $port = 22;
open (FILE, "</var/bak/test/iplist") or die "can't open file: $!\n";
while (<FILE>) {
print;
&ssh_host( "$_", "$port", "$user", "$pass" );
}
close(FILE);
sub ssh_host() {
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 => 5,
);
open (FH, ">> /var/bak/test/log_$host") or die "can't open file: $!\n";
print FH "-" x 80, "\n";
my $start_time = localtime;
print FH "start \tat $start_time\n";
$ssh->debug(0);
$ssh->run_ssh() or die "SSH process couldn't start: $!\n";
$ssh->waitfor( '\(yes\/no\)\?$', 5 );
$ssh->send("yes\n");
$ssh->waitfor( 'password:\s*$/', 5 );
$ssh->send("$pass");
$ssh->send("su - root");
$ssh->waitfor( 'Password:\s*$', 5 );
$ssh->send("$root_pass");
$ssh->waitfor( '#\s*', 5 );
print FH "root login ok. \n";
$ssh->send("passwd");
$ssh->waitfor( 'password:\s*$', 5 );
$ssh->send("$new_user_pass");
$ssh->waitfor( 'password:\s*$', 5 );
$ssh->send("$new_user_pass");
$ssh->waitfor( '#\s*', 5 );
my $ls = $ssh->exec("id");
print FH "$ls\n";
print FH "chang password ok!!!!!!!\n";
print FH "$new_user_pass\n";
my $end_time = localtime;
print FH "end \tat $end_time\n";
$ssh->close();
close FH;
print "-" x 80, "\n";
}
use strict;
use warnings;
use Net::SSH::Expect;
use String::MkPasswd qw(mkpasswd);
my $user = "user";
my $pass = 123456;
my $root_pass = 123456;
my $new_user_pass = mkpasswd(-length => 15, -minnum => 3, -minlower => 3 , -minupper => 3, -minspecial => 3);
my $port = 22;
open (FILE, "</var/bak/test/iplist") or die "can't open file: $!\n";
while (<FILE>) {
print;
&ssh_host( "$_", "$port", "$user", "$pass" );
}
close(FILE);
sub ssh_host() {
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 => 5,
);
open (FH, ">> /var/bak/test/log_$host") or die "can't open file: $!\n";
print FH "-" x 80, "\n";
my $start_time = localtime;
print FH "start \tat $start_time\n";
$ssh->debug(0);
$ssh->run_ssh() or die "SSH process couldn't start: $!\n";
$ssh->waitfor( '\(yes\/no\)\?$', 5 );
$ssh->send("yes\n");
$ssh->waitfor( 'password:\s*$/', 5 );
$ssh->send("$pass");
$ssh->send("su - root");
$ssh->waitfor( 'Password:\s*$', 5 );
$ssh->send("$root_pass");
$ssh->waitfor( '#\s*', 5 );
print FH "root login ok. \n";
$ssh->send("passwd");
$ssh->waitfor( 'password:\s*$', 5 );
$ssh->send("$new_user_pass");
$ssh->waitfor( 'password:\s*$', 5 );
$ssh->send("$new_user_pass");
$ssh->waitfor( '#\s*', 5 );
my $ls = $ssh->exec("id");
print FH "$ls\n";
print FH "chang password ok!!!!!!!\n";
print FH "$new_user_pass\n";
my $end_time = localtime;
print FH "end \tat $end_time\n";
$ssh->close();
close FH;
print "-" x 80, "\n";
}
相关文章推荐
- Linux下添加FTP账号和服务器、增加密码和用户,更改FTP目录(转)
- 更改windows AD 服务器密码后,无法再次修改密码解决方法
- Linux下添加FTP账号和服务器、增加密码和用户,更改FTP目录
- 建好ftp服务器后怎么从客户端更改密码?
- Linux下添加FTP账号和服务器、增加密码和用户,更改FTP目录
- Linux下添加FTP账号和服务器、增加密码和用户,更改FTP目录
- 如何更改服务器用户名和密码
- Exchange服务器系列课程之九--OWA更改密码
- 如何更改在local server本地服务器运行的Wordpress的密码PS
- Linux下添加FTP账号和服务器、增加密码和用户,更改FTP目录
- Window Server 2008 R2 在Active Directory域中不能更改服务器密码策略
- [Windows Server 2012] 更改服务器密码
- ahjesus linux连接阿里云ubuntu服务器并更改默认账号和密码,以及创建子账户
- ahjesus linux连接阿里云ubuntu服务器更改默认账号和密码、创建子账户的步骤
- Exchange服务器系列课程之九--OWA更改密码
- 如何更改服务帐户和服务帐户密码在 SharePoint 服务器 2007年和 Windows SharePoint Services 3.0
- Linux下添加FTP账号和服务器、增加密码和用户,更改FTP目录
- Window Server 2008 R2 在Active Directory域中不能更改服务器密码策略
- 服务器用户密码更改影响程序执行解决(之一)
- windows2003服务器更改管理员administrator用户名和密码的方法