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!

In reply to Re: Call vmstat remotely by thanos1983
in thread Call vmstat remotely by e123

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.