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

Hello folks,

I have recently moved from Net::SSH:Perl (which was a pain in the btt to install on solaris with no internet connection plus it would eat up my CPU when used because of Math::Pari which would not find the porper C library and would thererfore switch to a less performing perl library to do its job) to Net::OpenSSH.

Net::OpenSSH actually installs quite nicely but since I am using it, the ssh welcome message (the logon banner if you wish), which normally appears when you logon with ssh client manually, now appears in the script's STDERR.

The situation looks like this:

see below the sample code which causes this:
my $ssh = Net::OpenSSH->new( $remote_host, ( ssh_cmd => '/usr/local/bin/ssh', user => $user, password => $pass ) ); my $command = '/bin/ls -l'; my ($out, $err) = $ssh->capture2($command); chomp $out; undef $ssh;


Any idea why this happens ?

As a workaround, just before using OpenSSH I redirect STDERR to /dev/null (open STDERR, '>/dev/null';) and I close it when the SSH stuff is over.
That seems to work.

Thanks in advance

Max

PS This is my first question on perlmonks. I did my best to desccribe my problem in a brief but precise way so that others here may understand it.
If my behavior is somehow violating the ground rules of the forum please advise.

Replies are listed 'Best First'.
Re: Handling STDERR when using Net::OpenSSH
by salva (Canon) on Oct 14, 2010 at 10:13 UTC
    Add master_stderr_discard => 1 to the constructor call:
    my $ssh = Net::OpenSSH->new($remote_host, ssh_cmd => '/usr/local/bin/ssh', user => $user, password => $pass, master_stderr_discard => 1);
      Salva you are the best, thanks.

      And me, I am an idiot for not reading the API doc before posting.

      Max

      Hi, I know I'm replying to a bit of an old thread here, but I'm at a loss.

      I'm trying to use Net::OpenSSH 0.7.0 within an apache fastCGI script (not recommended, I know!).

      No matter what I do, I can't get it to run due to fastCGI messing with the standard filehandles.

      I get either:

      child process STDIN is not a real system file handle. or Operation 'OPEN' not supported on FCGI::Stream handle

      I've tried various combinations of :

      default_stdin_discard => 1, default_stdout_discard => 1, default_stderr_discard => 1, master_stderr_fh => $stderr_fh, master_stdout_fh => $stdout_fh,

      I've tried reopening STDIN from /dev/null locally.

      I'm pulling my hair out here.

      Any hints?

        Can you please describe (or show relevant code) of your setup in a bit more detail just so we can understand your situation better?

        I think your setup is something like the following:

        local machine remote machine script Net::OpenSSH fastCGI script

        ... and you want to run the fastCGI script and get its output?

        Maybe you can also show us how you're starting the fastCGI script exactly. Maybe FastCGI is happier with having actual filehandles (with inodes), or it wants a /dev/tty or a domain socket to interact with, instead what Net::OpenSSH provides directly.

        A nasty alternative could be to set up just enough of Apache (or another FastCGI environment using Net::FastCGI or whatever), and then access the script via http...