【问题标题】:Perl Net::SSH2 module not working on device listPerl Net::SSH2 模块在设备列表上不起作用
【发布时间】: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


【解决方案1】:

您必须在循环内创建 Net::SSH2 对象,因为无法使用一个 Net::SSH2 对象连接到多个主机(或执行到同一主机的多个连接)。

【讨论】:

    【解决方案2】:

    $chan->close();

    之后调用$ssh->disconnect()

    【讨论】:

      【解决方案3】:

      在身份验证失败时不要退出();使用 'next' 移动到下一个列表项。

      【讨论】:

      • 感谢您的回复,但我已更改为“下一步”但仍无法连接到其他设备。
      【解决方案4】:

      只需将 my $ssh2 = Net::SSH2->new(); 放在 while 循环下即可。

      【讨论】:

      • 请在回答问题时尝试提供尽可能多的信息。 (例如:这样做会达到什么效果,如何纠正问题等)
      【解决方案5】:
         #!\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;
      

      【讨论】:

      • 什么解决了您的问题?只是转储有效的代码并期望有人对其进行比较是不可接受的。
      • @CanSpice@ 我已经发布了代码,因为如果有人想使用该代码。我有没有要求任何人找到差异。你为什么要打扰,因为我已经发布了工作代码。
      • @Daniel 你必须为你的答案提供解释。首先,你必须了解堆栈溢出的重要性。
      猜你喜欢
      • 1970-01-01
      • 2018-03-14
      • 1970-01-01
      • 2018-10-02
      • 2012-06-14
      • 2012-09-28
      • 2017-02-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多