Creating array over SSH in Perl

Home > Search
  by

Let's say you make an SSH connection to another host in Perl and obtain some data, such as a list of files.

($stdout) = $ssh->cmd("ls");

 

In this example, the list of files from the remote host is stored in the $stdout variable. The variable can be printed.

print $stdout;

 

Printing this variable will print the list of files on the remote host.

file1.txt
file2.txt
file3.txt

 

The dumper function validates that the entire list of files in a single variable value.

use Data::Dumper;
print Dumper $stdout;

 

In other words, each files is not associated with a unique variable value.

$VAR1 = 'file1.txt
         file2.txt
         file3.txt';

 

Split can be used to create an array, where each file is a unique value in the array.

@files = split(/\n/, $stdout);

 

And now you should be able to print each item in the array.

print @files;

 

Which will print each file.

file1.txt file2.txt file3.txt

 

Dumper can be used to verifiy each item in the array is unique.

print Dumper @files;

 

The dumper output.

$VAR1 = 'file1.txt';
$VAR2 = 'file2.txt';
$VAR3 = 'file3.txt';

 

 



Add a Comment




We will never share your name or email with anyone. Enter your email if you would like to be notified when we respond to your comment.




Please enter in the box below so that we can be sure you are a human.




Comments