我是Windows Perl的新手,我正在Net::SSH2上尝试使用Strawberry Perl。我有脚本的问题是无法连接到设备列表。我可以连接到列表中的第一个设备,但无法连接到第二个,第三个等等。我错过了什么。谢谢你的任何建议。
#!\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 = "XXXXX"; # 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 $_;
unless ($ssh2->connect("$_")) {
print "Unable to connect : $_\n";
next;
}
my $dp=decode_base64("$pass");
unless ($ssh2->auth_password("$user","$dp")) {
print "Invalid Password\n";
exit;
}
my $chan = $ssh2->channel();
$chan->exec('sh ver');
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) or die "$!";
print OUTPUT "HOST: $_\n\n";
print OUTPUT "$buf\n";
print OUTPUT "\n\n\n";
print OUTPUT
$chan->close();
}
close (List);
答案 0 :(得分:1)
您必须在循环内创建Net :: SSH2对象,因为无法使用一个Net :: SSH2对象连接到多个主机(或执行多个连接到同一主机)。
答案 1 :(得分:1)
在 $ chan-&gt; close();
之后致电 $ ssh-&gt; disconnect()答案 2 :(得分:0)
请勿在身份验证失败时退出();使用'next'移动到下一个List项目。
答案 3 :(得分:0)
只需在while循环下放置 my $ ssh2 = Net :: SSH2-&gt; new(); 。
答案 4 :(得分:-3)
#!\usr\bin\Perl\bin\perl
use strict;
use Term::ReadKey;
use NET::SSH2;
use MIME::Base64;
use constant BUFLEN => 10_0000 ;
my $user = "XXX"; # your account
my $pass = "XXXX"; # your password 64 bit mime
my $dp=decode_base64("$pass");
my $host = "C:/temp/devices.txt"; # input file
my $Error = "C:/temp/Error.txt"; # Error file
open(HOST, '<', "$host") or die "$!";
open STDERR, ">", "$Error"; # open log file
while(<HOST>) {
chomp $_;
my $ssh2 = Net::SSH2->new();
$ssh2->debug(1); # debug on/off
unless ($ssh2->connect("$_")) {
print "Unable to connect : $_\n";
print STDERR "Unable to connect to $_: $!\n"; # write the error on log file
print STDERR
"*****************************************************\n\n";
next;
}
print "connecting to $_\n";
unless ($ssh2->auth_password("$user","$dp")) {
print "Invalid Password\n";
exit;
}
my $chan = $ssh2->channel;
$chan->exec('sh int desc');
my $buf;
my $read = $chan->read($buf, BUFLEN );
warn 'More than ', BUFLEN, ' characters in listing' if $read >= BUFLEN;
open (OUTPUT, ">", "C:/temp/$_.txt")or die "$!"; # new file for each devices
print OUTPUT "HOST: $_\n\n";
print OUTPUT "$buf\n";
print OUTPUT "\n\n\n";
print OUTPUT
$chan->close();
}
close HOST;