阻止NET :: SSH2打印不需要的数据

时间:2017-02-01 11:04:02

标签: perl perlnetssh

我正在尝试使用NET :: SSH从perl执行远程Ubuntu系统中的命令。我得到了所需的输出,但是随之而来的是脚本正在打印大量不需要的数据。我们如何阻止它。提前谢谢。

以下是不需要的数据样本(每次执行时都会打印数百次)。

Net::SSH2::Channel::read(size = 1, ext = 0)
- read 1 bytes
- read 1 total Net::SSH2::poll: timeout = 250, array[1]
- [0] = channel
- [0] events 1
- libssh2_poll returned 1
- [0] revents 1 Net::SSH2::Channel::read(size = 1, ext = 0)
- read 1 bytes
- read 1 total Net::SSH2::poll: timeout = 250, array[1]
- [0] = channel
- [0] events 1
- libssh2_poll returned 1
- [0] revents 1 Net::SSH2::Channel::read(size = 1, ext = 0)
- read 1 bytes
- read 1 total

我的代码是

use warnings;
use strict;
use NET::SSH2;
use Data::Dumper;
my $auth;
sub is_sshalive;

my $host = "";               # use the ip host to connect
my $user = "ubuntu"; # your account
my $cmd;
my $ssh2 = Net::SSH2->new();
$ssh2->debug(1);
$ssh2->connect($host,"22") or die "connect failed";
$ssh2->auth_publickey(
       'ubuntu',
       'publickey',
       'privatekey'

    ) or die "auth failed";


        print "\n Executing command...\n";

        $cmd = "ls -al";
        print " ==> Running $cmd\n";

        if(is_sshalive($ssh2) == 1) {
                print "\nSSH connection died";
                exit 1;
        } else {

         run_testsuite($cmd, $ssh2);
        # echo $ssh2->exec("ls");

        }

print "test passed done 0\n";


sub run_testsuite {
    my $cmd = $_[0];
    my $ssh2 = $_[1];


    my $chan2 = $ssh2->channel();
    $chan2->shell();
    my @arr;
    print $chan2 "$cmd \n";
    print $chan2 "pwd \n";
    push @arr,$_  while <$chan2>;
    print @arr;


    $chan2->close;
    return 0;
}

sub is_sshalive {
   my $ssh2 = $_[0];
    print "isalive-shiva";
    if ($ssh2->poll(1000) == 0) {
        return 0; # passed
    } else {
        return 1; #failed
    }
    return 0;
}

1 个答案:

答案 0 :(得分:1)

$ssh2->debug(1);更改为$ssh2->debug(0);,现在打印所有相应的数据。