2011-12-14 17 views
0

我是Windows Perl的新手,我试图在Strawberry Perl上使用Net::SSH2。我有脚本的问题无法连接到设备列表。我可以连接到列表中的第一个设备,但无法连接到第二个,第三个等。我错过了什么?感谢您的任何建议。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); 
+1

这应该是`使用网络:SSH2`不使用NET :: SSH2`。它只能工作,因为Windows不区分大小写。 – 2011-12-14 22:43:04

+0

你得到什么错误信息? – CanSpice 2011-12-14 22:47:38

+0

@Brad Gilbert @我同意错字错误。 – Daniel 2011-12-14 23:11:55

回答

0

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

1

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

1

请致电$ ssh-> disconnect()$ chan-> close();

-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; 
0

简而言之我的$ SSH2 =净:: SSH2->新();在while循环下的

相关问题