使用Net :: SSH2 Perl模块从HP Switch检索输出

时间:2017-10-03 07:59:17

标签: windows perl ssh

enter image description here我正在尝试使用Net :: SSH2 Perl模块(Windows操作系统)通过SSH连接到HP Switch。该脚本能够连接并验证设备,但不会检索输出。我的输出空白了。

我尝试过使用Net :: SSH2 shell并执行这两种操作,但直到现在都没有运气。 也试过Net :: SSH :: Any,仍然没有结果。

Hp Switch型号:HP 5900 - 7.1.045

代码

use strict;
use Net::SSH2;

my $ssh = Net::SSH2->new();

$ssh->connect('xx.xx.xx.xx');

$ssh->auth(username => 'xxxxx', password => 'xxxxxx');

my  $channel = $ssh->channel() or die 'Error creating channel';

$channel->blocking(0);

$channel->shell();

print $channel "display supervlan\n";       

sleep(5);      

 while(<$channel>)
   {
      print $_;
   }

$channel->close(); 

$ssh->disconnect();

有人可以帮忙吗。

1 个答案:

答案 0 :(得分:1)

Net::SSH::Any可以使用任何可用的后端ssh模块(在windows下使用Net :: SSH2,如果可用)

use Net::SSH::Any;

my $ssh = Net::SSH::Any->new(
    "127.0.0.1",
    port => "22",
    user => "xx",
    password => "xx",
    strict_host_key_checking =>0
);
$ssh->error and die $ssh->error;

my ($out, $err) = $ssh->capture2("ls -l /");
print $out;
相关问题