Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

getting STDERR from external command using OPEN

by gabrielsousa (Sexton)
on Nov 09, 2016 at 01:13 UTC ( [id://1175571]=perlquestion: print w/replies, xml ) Need Help??

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 (Archbishop) 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 (Prior) 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; }

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1175571]
Approved by stevieb
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (3)
As of 2024-04-20 13:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found