perl使用 Net::SSH::Expect修改主机密码

#!/usr/bin/perl
use strict;
use warnings;
use Net::SSH::Expect;
my @ssh_list;
while (<>) {
@ssh_list=split/\t+/,$_;
print $ssh_list[0]."正在修改密码\n";
&ssh_test("$ssh_list[0]","$ssh_list[1]","$ssh_list[2]","$ssh_list[3]","$ssh_list[4]");
}
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,
);
$ssh->debug(0);
$ssh->run_ssh() or die "SSH process couldn't start: $!";
$ssh->waitfor( '\(yes\/no\)\?$', 2 );
$ssh->send("yes\n");
$ssh->waitfor( 'password:\s*$/', 2);
$ssh->send("$ssh_list[3]");
$ssh->waitfor( '#\s', 2 );
$ssh->send("passwd $ssh_list[2]");
$ssh->waitfor( 'password:\s*$', 2 );
$ssh->send("$ssh_list[4]");
$ssh->waitfor( 'password:\s*$', 2 );
$ssh->send("$ssh_list[4]");
$ssh->waitfor( '#\s*', 2 );
$ssh->close();
print "修改完成\n";
print "-" x 30, "\n";
}