【发布时间】:2011-12-14 20:13:02
【问题描述】:
我是 Windows Perl 的新手,我正在尝试在 Strawberry Perl 上使用 Net::SSH2。我有脚本的问题是无法连接到设备列表。我可以连接到列表中的第一个设备,但无法连接到第二个、第三个等等。我错过了什么吗?感谢您的任何建议。
#!\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);
【问题讨论】:
-
应该是
use Net:SSH2而不是use NET::SSH2。它之所以有效,是因为 Windows 不区分大小写。 -
您收到了哪些错误消息?
-
@Brad Gilbert@我同意打字错误。
-
你可以完全删除你的shebang
#!\usr\bin\Perl\bin\perl,因为1)它是用反斜杠写的,2)Windows不需要它,或者使用它,3)你使用的是linux路径.您的 perl 路径更可能类似于C:/perl/bin/perl.exe
标签: perl perl-module