perl系统命令执行

Raj*_*eev 0 ssh perl

在下面的代码,我需要打印从变量输出$stout,$stderr.我怎么能这样做,而不是没有使用$ssh = Net::SSH::Perl->new

#!/usr/bin/perl
use strict;
use warnings;
use Net::SSH::Perl;
use FindBin qw($RealBin);
use File::Basename;
use lib "/nfs/site/proj/dpg/tools/lib/perl";
use Util;
use Getopt::Std;
use Net::SSH::Perl;
use Cwd;
use File::Copy;

my $host = 'someip.com.com';
my $pass = '';
my $user = '';
my $cmd  = 'pwd';

my($stdout,$stderr,$exit) = system('ssh someip.com cat /nfs/site/home/aa/test11.txt')        ;
if ($stdout) {
    print "Standard output is \n$stdout";
} elsif($stderr) {
    print "Standard error is \n$stderr";
}
Run Code Online (Sandbox Code Playgroud)

jch*_*s12 5

我使用IO :: CaptureOutput,它更简单.

use strict;
use warnings;

use IO::CaptureOutput qw(capture_exec);

my $cmd = "ssh someip.com cat /nfs/site/home/aa/test11.txt";
my ($stdout, $stderr, $success, $exitcode) = capture_exec( $cmd );
Run Code Online (Sandbox Code Playgroud)

您还可以使用capture_exec我认为更安全的列表参数.