#!/usr/bin/perl use strict; use warnings; use Net::OpenSSH; use Data::Dumper; my $host = "127.0.0.1"; # remote IP change my $port = 22; # ssh default port my $passwd = "password"; my $user = "username"; my %opts = ( passwd => $passwd, port => $port, user => $user ); sub loop { my $ssh = Net::OpenSSH->new( $host , %opts ); $ssh->error and die "Couldn't establish SSH connection: ". $ssh->error; my @final = (); for (1..3) { $ssh->system("vmstat") or die "remote command failed: " . $ssh->error; my @output = $ssh->capture("vmstat"); $ssh->error and die "remote ls command failed: " . $ssh->error; push (@final,@output); } chomp(@final); return @final; } # end sub loop my @array = loop(); print Dumper(\@array); __END__ procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu----- r b swpd free buff cache si so bi bo in cs us sy id wa st 1 0 341104 448888 57136 1158016 5 23 85 85 560 1618 23 5 71 1 0 procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu----- r b swpd free buff cache si so bi bo in cs us sy id wa st 2 0 341104 448504 57136 1158016 5 23 85 85 560 1618 23 5 71 1 0 procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu----- r b swpd free buff cache si so bi bo in cs us sy id wa st 1 0 341104 459728 57136 1147676 5 23 85 85 560 1618 23 5 71 1 0 $VAR1 = [ 'procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu-----', ' r b swpd free buff cache si so bi bo in cs us sy id wa st', ' 2 0 341104 448472 57136 1158016 5 23 85 85 560 1618 23 5 71 1 0', 'procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu-----', ' r b swpd free buff cache si so bi bo in cs us sy id wa st', ' 1 0 341104 459312 57136 1147676 5 23 85 85 560 1618 23 5 71 1 0', 'procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu-----', ' r b swpd free buff cache si so bi bo in cs us sy id wa st', ' 1 0 341104 459568 57136 1147676 5 23 85 85 560 1618 23 5 71 1 0' ];