#!/usr/bin/perl -w use strict;
use Expect;
#passwd.txt格式如下:
#host port user pass
if(@ARGV != 2) {
print "Usage:\n";
print "perl ssh_expect.pl passwd.txt host\n";
exit 1;
}
my $passwd = $ARGV[0];
my $ip = $ARGV[1];
open(FILE,$passwd)||die"can not open the file: $passwd";
while (defined (my $line =)) {
chomp $line;
if ($line =~ /$ip/) {
my @array = split(/\s+/,$line);
&ssh_expect($array[0],$array[1],$array[2],$array[3]);
}
}
sub ssh_expect() {
my $host=$_[0];
my $port=$_[1];
my $user=$_[2];
my $pass=$_[3];
my $exp = new Expect;
$exp=Expect->spawn( "ssh -p$port $user\@$host" );
$exp->expect(10,[ qr/\(yes\/no\)\?\s*$/ => sub { $exp->send("yes\n"); exp_continue; } ],);
$exp->expect(10, "assword:" );
$exp->send( "$pass\n" );
$exp->interact();
}
close FILE;