2017-10-07 Note added by Athanasius: This node was copied from StackExchange. See also Re: Extract specify values in file log using 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->new("$username\@$s", timeout=>30); $ssh->error and die "Unable to connect: " . $ssh->error; print "Connected to $s\n"; my $fh = $ssh->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->capture("svmon -G -O unit=GB | grep memory") or die "U +nable 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?


In reply to Remotely collect server data using Net::OpenSSH Perl by IruP

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.