我有這個腳本來讀取設備列表併發送命令。但目前它只讀取第一個設備併發送命令,忽略其餘部分。我錯過了什麼?在多個設備上發送命令
#!\usr\bin\Perl\bin\perl
use warnings;
use strict;
use NET::SSH2;
use MIME::Base64;
my $host = "C:/temp/devices.txt"; # input file
my $user = "XXX"; # your account
my $pass = "XXXXXX"; # your password 64 bit mime
my $ssh2 = Net::SSH2->new();
my $result = "C:/temp/result.txt"; # output file
$ssh2->debug(1); # debug on/off
open(List, '<', "$host") or die "$!";
while(<List>) {
chomp $_;
$ssh2->connect("$_") or die "Unable to connect host [email protected] \n";
my $dp=decode_base64("$pass");
$ssh2->auth_password("$user","$dp");
my $chan = $ssh2->channel();
$chan->exec('sh run');
my $buflen =100000;
my $buf = '0' x $buflen;
my $read = $chan->read($buf, $buflen);
warn 'More than ', $buflen, ' characters in listing' if $read >= $buflen;
open OUTPUT, ">", "$result";
print OUTPUT "HOST: $_\n\n";
print OUTPUT "$buf\n";
print OUTPUT "\n\n\n";
print OUTPUT
close (List);
$chan->close();
}
謝謝,這解決了第一個問題。讀取第二個設備列表,但現在無法連接到第二個設備出現錯誤「無法連接到主機」 – Daniel 2011-12-14 19:17:06