0

In the following code I need to print output from the variables $stout, $stderr. How can I do this and not without using $ssh = Net::SSH::Perl->new?

#!/usr/bin/perl
use strict;
use warnings;
use Net::SSH::Perl;
use FindBin qw($RealBin);
use File::Basename;
use lib "/nfs/site/proj/dpg/tools/lib/perl";
use Util;
use Getopt::Std;
use Net::SSH::Perl;
use Cwd;
use File::Copy;

my $host = 'someip.com.com';
my $pass = '';
my $user = '';
my $cmd  = 'pwd';

my($stdout,$stderr,$exit) = system('ssh someip.com cat /nfs/site/home/aa/test11.txt')        ;
if ($stdout) {
    print "Standard output is \n$stdout";
} elsif($stderr) {
    print "Standard error is \n$stderr";
}
6
  • According to the perldoc, system returns the exit code, so your command system('ssh...' will probably put the exit code is $stdout and leave $stderr and $exit undefined. What values are you expecting in those variables? Commented Mar 19, 2012 at 10:01
  • $stdout i am expecting the contents of the file and if the file doest exist i am expecting the error message in stderr Commented Mar 19, 2012 at 10:04
  • If you run it in backticks, rather than system, it will return the output, but I'm not sure how you distinguish between succesful output (the file) and unsuccesful output (an error message). $output = `ssh someip.com...` Commented Mar 19, 2012 at 10:11
  • Can we get some standard ouput or standarderr from backtiks or system command. Commented Mar 19, 2012 at 10:22
  • @ Matt Freake:I am implementing the way u have told but have accepted the other answer since it can be done that way too.No hard feelings :) Commented Mar 19, 2012 at 11:11

3 Answers 3

5

I use IO::CaptureOutput, its simpler.

use strict;
use warnings;

use IO::CaptureOutput qw(capture_exec);

my $cmd = "ssh someip.com cat /nfs/site/home/aa/test11.txt";
my ($stdout, $stderr, $success, $exitcode) = capture_exec( $cmd );

You can also use list parameters in capture_exec which I think is safer.

Sign up to request clarification or add additional context in comments.

Comments

1

open3 allows you to read/write all handles:

use FileHandle;
use IPC::Open3;
my $cmd = "ssh someip.com cat /nfs/site/home/aa/test11.txt";
open3(\*GPW, \*GPR, \*GPE, "$cmd") or die "$cmd";
    # print GPW "write to ssh";
close GPW;
while (<GPR>) { 
  # read stdout
}
while (<GPE>) { 
  # read stderr
}
close GPR; close GPE;

Comments

0

I've always been a fan of IPC::Run:

use IPC::Run;

my $exitcode = run [ "ssh", "someip.com", "cat", ... ],
    undef, \my $stdout, \my $stderr;

At this point the STDOUT and STDERR results from the command will be stored in those two lexicals.

Though as a solution to the general issue of ssh'ing to a host and retrieving the contents of a file from within a Perl program, you might like IPC::PerlSSH:

use IPC::PerlSSH;

my $ips = IPC::PerlSSH->new( Host => "someip.com" );
$ips->use_library( "FS", qw( readfile ) );

my $content = $ips->call( "readfile", "/nfs/site/home/aa/test11.txt" );

The $ips object here will just hang around and allow for reuse to collect multiple files, execute other commands, and generally reuse the connection, rather than having to set up a new ssh connection every time.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.