http://www.perlmonks.org?node_id=1175571

gabrielsousa has asked for the wisdom of the Perl Monks concerning the following question:

how to i get STDERR from this ?

open(SSH,"sshpass -p $pass ssh user\@$ip $cmd |") || die "$!\n";
@result= <SSH>; chomp @result;
close SSH;
  • Comment on getting STDERR from external command using OPEN

Replies are listed 'Best First'.
Re: getting STDERR from external command using OPEN
by kcott (Bishop) on Nov 09, 2016 at 03:48 UTC
Re: getting STDERR from external command using OPEN (open2/open3/Capture::Tiny)
by Anonymous Monk on Nov 09, 2016 at 03:31 UTC

    For what you want to do Capture::Tiny is the best

    use Capture::Tiny qw/ capture /; my @cmd = ( "...ls", 'dir' ); my( $stdout, $stderr, $exit ) = capture { system { $cmd[0] } @cmd; };; $exit and die "it failed with $exit and $stderr ";

    The alternatives are IPC::Open2, IPC::Open3...

      thanks , i have perl-Capture-Tiny on Cygwin :)
Re: getting STDERR from external command using OPEN
by marioroy (Parson) on Nov 10, 2016 at 01:12 UTC

    Updated: November 9, 2016

    The following is a cross-platform demonstration for capturing STDERR.

    use strict; use warnings; use File::Temp qw/ tempfile /; # Initially, borrowed bits from IPC::Run3 for capturing STDERR. # Then, made efficient for lesser overhead than Capture::Tiny. sub do_cmd { my ( $cmd ) = @_; my ( undef, $err_file ) = tempfile(); my ( $err, $err_fh, @result ) = (''); if ( $^O eq 'MSWin32' ) { # this is one way to capture STDERR, also works on Unix open my $cmd_fh, "$cmd 2> $err_file |" or die "$!\n"; @result = <$cmd_fh> if defined ( fileno $cmd_fh ); close $cmd_fh; } else { # this is another way, but not supported on Windows local *STDERR_SAVE; open STDERR_SAVE, '>&STDERR'; open $err_fh, '+>', $err_file; binmode $err_fh, ':raw'; open STDERR, '>&' . fileno $err_fh; open my $cmd_fh, "$cmd |" or die "$!\n"; @result = <$cmd_fh> if defined ( fileno $cmd_fh ); close $cmd_fh; open STDERR, '>&STDERR_SAVE'; close $err_fh; } if ( -s $err_file ) { open $err_fh, '<', $err_file; local $/ = undef; $err = <$err_fh>; close $err_fh; } unlink $err_file; return ( $err, @result ); } # Demonstration. my $pass = 'password'; my $ip = 'ip_address'; my $cmd = 'uptime'; my ($err, @result) = do_cmd("sshpass -p $pass ssh user\@$ip $cmd"); if (length $err) { print "ERROR: $err\n"; } else { chomp @result; }