Please understand that I'm not currently in the position to test what I'm writing below, but you can get the idea.

If you mandatorily want to use mail or mailx, you'd probably do that using the shell instead of Perl:

#!/bin/bash while read command ; do rsh $command snapmirror status done <netapps 2>&1 | mail foo@example.com
or something like that (I don't know the mail program).

OTOH, Perl has a plethora of modules dealing with e-mail, which you can find on http://www.cpan.org (or, better, on http://search.cpan.org), among which I've used Jenda's Mail::Sender which I find both simple and effective. If you can go with a full-Perl solution, you have to get rid of the system call and use the backtick operator or its equivalent qx operator; in this case perlop will help you but it should more or less sound like this:

#!/usr/bin/perl use strict; use warnings; open IN, "<", "netapps" || die "Can't open file"; my $output = ''; while (<IN>) { chomp; $output .= qx( rsh $_ snapmirror status 2>&1 ); }
Note the redirection to be sure to grab both stdout and stderr. Now $output should hold both of them from all commands, and you can send it by email.

As a final note, be absolutely sure that you trust the netapps file, otherwise you will have bad, bad surprises.

Update: removed backslash inside qx()

Flavio (perl -e 'print(scalar(reverse("\nti.xittelop\@oivalf")))')

Don't fool yourself.

In reply to Re: capture output and email on linux by polettix
in thread capture output and email on linux by Anonymous Monk

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.