Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked

Re: getting STDERR from external command using OPEN

by marioroy (Parson)
on Nov 10, 2016 at 01:12 UTC ( #1175651=note: print w/replies, xml ) Need Help??

in reply to getting STDERR from external command using OPEN

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

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1175651]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (6)
As of 2021-10-21 15:20 GMT
Find Nodes?
    Voting Booth?
    My first memorable Perl project was:

    Results (83 votes). Check out past polls.