Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Handling STDERR when using Net::OpenSSH

by mds (Novice)
on Aug 20, 2013 at 16:01 UTC ( #1050204=perlquestion: print w/ replies, xml ) Need Help??
mds has asked for the wisdom of the Perl Monks concerning the following question:

Is it possible to capture the output of stderr in Net::OpenSSH to a variable instead of to a file? In this particular case I want the stderr of the following "new" function not the stderr of commands once I have the connection established.

my $ssh = Net::OpenSSH->new($host); $ssh->error and die "Couldn't establish SSH connection: ". $ssh->error;

ssh->error does not provide all the information I need during a connection failure.
I get different reasons for failure to connect. Some are "administratively denied" other are just "disconnect" and all these messages go to stderr. It looks like I can capture the stderr to a file but I would like to avoid that if possible.

Comment on Handling STDERR when using Net::OpenSSH
Download Code
Re: Handling STDERR when using Net::OpenSSH
by jandrew (Hermit) on Aug 20, 2013 at 17:23 UTC
    Are you familiar with $SIG{__WARN__} handlers?

    Update: moved the link to point directly at %SIG

Re: Handling STDERR when using Net::OpenSSH
by kevbot (Hermit) on Aug 21, 2013 at 04:45 UTC
    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.
      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.
Re: Handling STDERR when using Net::OpenSSH
by salva (Monsignor) on Aug 26, 2013 at 09:00 UTC
    Is it possible to capture the output of stderr in Net::OpenSSH to a variable instead of to a file

    Not safely.

    The master SSH process is run on the background asynchronously. Any module redirecting its ouput to anything but a file may interfere with the module inner workings.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1050204]
Front-paged by Arunbear
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (16)
As of 2014-07-22 17:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (122 votes), past polls