Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re: Handling STDERR when using Net::OpenSSH

by kevbot (Chaplain)
on Aug 21, 2013 at 04:45 UTC ( #1050277=note: print w/ replies, xml ) Need Help??


in reply to Handling STDERR when using Net::OpenSSH

The Capture::Tiny module can be used to capture STDERR. For example,

#!/usr/bin/env perl use strict; use warnings; use Net::OpenSSH; use Capture::Tiny ':all'; my $host = 'broken'; my $ssh; my ($stdout, $stderr, @result) = capture { $ssh = Net::OpenSSH->new($host); }; print "Captured STDERR: $stderr\n"; $ssh->error and die "Couldn't establish SSH connection: ". $ssh->error +; exit;
gives the output:
Captured STDERR: ssh: Could not resolve hostname broken: nodename nor +servname provided, or not known Couldn't establish SSH connection: unable to establish master SSH conn +ection: master process exited unexpectedly at ns.pl line 15.


Comment on Re: Handling STDERR when using Net::OpenSSH
Select or Download Code
Replies are listed 'Best First'.
Re^2: Handling STDERR when using Net::OpenSSH
by mds (Novice) on Aug 21, 2013 at 13:39 UTC
    Thank you kevbot and jandrew. I could not get $SIG{__WARN__} to provide any satisfaction in captureing STDERR. Capture::Tiny worked very well. Just to be sure and to keep it from being confused with the openssh capture function I used Capture::Tiny::capture
    my $ssh; my $LOGIN_TIMEOUT = 60; my ( $ssh_stdout, $ssh_stderr, @ssh_result ) = Capture::Tiny::capture { $ssh = Net::OpenSSH->new( $ip_address, batch_mode => 1, timeout => $LOGIN_TIMEOUT, kill_ssh_on_timeout => 1, master_opts => [ -o => "StrictHostKeyChecking=no", -o => "ConnectTimeout=60", -o => "ForwardX11=no" ], ); };
    Thank you very much.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (7)
As of 2015-07-29 07:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (260 votes), past polls