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

I'm using Net::SFTP::Foreign with plink as the SSH command: $sftp=Net::SFTP::Foreign->new("mysite.com", user=>"myuser" ,ssh_cmd=>'plink' ,more => -pw => "mypw" ); If I have a connection error like a bad password, the plink command returns a message like "Access denied" to STDERR. But $sftp->error contains the message "Connection to remote server is broken", which isn't very useful to a support person looking at a log. Is there a way to capture the output of the plink command inside my program?
  • Comment on How to capture Net::SFTP::Foreign output

Replies are listed 'Best First'.
Re: How to capture Net::SFTP::Foreign output
by kennethk (Abbot) on Mar 31, 2010 at 15:26 UTC
    You can capture or redirect the STDERR channel at will, just like any filehandle. This should work for operations that are shelled out as well. Details are in open, but a simple bit of code looks like:

    #!/usr/bin/perl use warnings; use strict; open my $olderror, ">&STDERR" or die "Can't dup STDERR: $!"; open STDERR, ">&STDOUT" or die "Can't dup STDOUT: $!"; warn "This is sent on STDOUT\n"; open STDERR, ">&", $olderror or die "Can't dup \$olderror: $!"; warn "This is sent on STDERR\n";

    Note STDERR, like all bareword filehandles, is global in scope and so you might be messing in someone else's backyard. Note as well you'll probably want to restore the channel, as I have, when you are done with it.

Re: How to capture Net::SFTP::Foreign output
by salva (Canon) on Apr 01, 2010 at 16:30 UTC