anurag86
anurag86

Reputation: 1687

How to store output of a command run remotely in an array in perl

I know this kind of question has been asked before, but I think I may have got lost trying to understand the examples provided in the responses. So I am asking it here again.

I have to collect list of files(absolute filenames only) from a particular folder on a remote machine.

On my Linux machine:

opendir - works locally. I want it to work remotely.

use Net::FTP - works but FTP disabled.

use Net::SFTP - could work but this Net::SFTP not installed.

I want some way to get this info considering that I have to make use of either sftp or ssh.

my $cmd = "ssh user\@host 'find x/y/z -type f'";
my @expectedOutputHere = system($cmd);

But $expectedOutputHere doesn't contain the list of files(as output). Once I have the output I am planning to use File::Basename::basename to get absolute names. But how do I first collect the output in an array?

Upvotes: 0

Views: 694

Answers (2)

anurag
anurag

Reputation: 202

Try this:

my $cmd = "ssh user\@host 'find x/y/z -type f'";
my @expectedOutputHere = `$cmd`;
chomp(@expectedOutputHere);
print Dumper @expectedOutputHere;

Upvotes: 1

Alexander Gelbukh
Alexander Gelbukh

Reputation: 2240

my @expectedOutputHere = `$cmd`;

The manual says: The collected standard output of the command is returned, In list context, returns a list of lines.

For system(), the manual specifies: The return value is the exit status of the program.

Upvotes: 4

Related Questions