#!/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")ordie"can′topenfile:!\n";
print FH "-" x 80, "\n";
my $start_time = localtime;
print FH "start \tat $start_time\n";
$ssh->debug(0);
ssh−>runssh()ordie"SSHprocesscouldn′tstart:!\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")ordie"can′topenfile:!\n";
print FH "-" x 80, "\n";
my $start_time = localtime;
print FH "start \tat $start_time\n";
$ssh->debug(0);
ssh−>runssh()ordie"SSHprocesscouldn′tstart:!\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";
}
本文转自 trt2008 51CTO博客,原文链接:http://blog.51cto.com/chlotte/284679,如需转载请自行联系原作者