Remotely collect server data using Net::OpenSSH Perl I am newbie in Perl programming and currently trying to use Net::OpenSSH module in my code, my new code as below which the task is to run multiple command in remote server,

Code as below::

foreach $s (@servers) {
my $ssh = Net::OpenSSH-&gt;new("$username\@$s", timeout=&gt;30);
$ssh-&gt;error and die "Unable to connect: " . $ssh-&gt;error;
print "Connected to $s\n";

my $fh = $ssh-&gt;capture("df -k /home/ | tail -1") or die "Unable to run command\n";
my @df_arr = split(/\s+/, $fh);
print "$s:  Disk space /home/ = $df_arr[3] \n";

my $fh1 = $ssh-&gt;capture("svmon -G -O unit=GB | grep memory") or die "Unable to run command\n";
my @sv_arr = split(/\s+/, $fh1);
print "$s:  Free memory = $sv_arr[3] \n\n";

close $fh;
undef $ssh;
}


This code is not so nice since I plan to simplify and reduce a line as many as possible.

Does there are any techniques or methods that I can use to simplify this code?
Looks good to me. Kudos for the "... or die" idiom. More informative output would be "unable to run df on $s" or "unable to run svmon on $s".

You don't have to do tail or grep on the far end, as a line of perl could do that. But it is a perfectly clean and sensible approach, which I advocate keeping.

If you wind up with many such monitoring commands, you may find it convenient to move the post-processing into perl commands that can be refactored out as utility functions.