http://qs1969.pair.com?node_id=1102059


in reply to Call vmstat remotely

Hello e123,

Welcome to the community.

If I understand correctly you want to execute a command remotely to a linux host and get the output. A possible solution would be ssh with Net::OpenSSH module.

Update: Sorry I forgot completely the part that you need to make it 3 times and take the average.

Update 2: I thought a bit about it and I came up with a more programmable solution. I am updating the code, I hope you are still following your question. From my point of view this a more correct answer because you are applying 3 times the same command and you force the system to process the command 3 times.

Updated code: Sample of code with command running on my OS

#!/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 s +y 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 s +y 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 s +y 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---- -sy +stem-- ------cpu-----', ' r b swpd free buff cache si so bi bo i +n cs us sy id wa st', ' 2 0 341104 448472 57136 1158016 5 23 85 85 5 +60 1618 23 5 71 1 0', 'procs -----------memory---------- ---swap-- -----io---- -sy +stem-- ------cpu-----', ' r b swpd free buff cache si so bi bo i +n cs us sy id wa st', ' 1 0 341104 459312 57136 1147676 5 23 85 85 5 +60 1618 23 5 71 1 0', 'procs -----------memory---------- ---swap-- -----io---- -sy +stem-- ------cpu-----', ' r b swpd free buff cache si so bi bo i +n cs us sy id wa st', ' 1 0 341104 459568 57136 1147676 5 23 85 85 5 +60 1618 23 5 71 1 0' ];

I am not calculating the average for you I left this part so you can play around and practice a bit. ;)

I execute the code on my localhost so I see double the output because the actual command is executed on my terminal. Which will happen the same on the remote terminal.

Hope this helps, let me know how it worked for you.

Seeking for Perl wisdom...on the process of learning...not there...yet!