Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I am running this script:

#!/usr/bin/perl -w + open (IN, "<netapps" ) || die "Can't open file"; + while(<IN>) { chomp; system("rsh $_ snapmirror status"); }

I want to capture the output and mail it using mail or mailx what is the easiest way? thanks!! <KRI>

Replies are listed 'Best First'.
Re: capture output and email on linux
by polettix (Vicar) on Jun 09, 2005 at 17:35 UTC
    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.
Re: capture output and email on linux
by cool_jr256 (Acolyte) on Jun 09, 2005 at 17:23 UTC
    What is the easiest way to capture output or mailing it or both?? For mailing you can use this:
    open(SENDMAIL, "|/usr/sbin/sendmail -t") || die "Cannot open sendmail +: $!"; print SENDMAIL "From: some_email\@somewhere.com\n"; print SENDMAIL "To: some_email\@somewhere.com\n"; print SENDMAIL "Subject: $some_message\n"; print SENDMAIL "\n\n"; print SENDMAIL $some_content; close (SENDMAIL) or die "Cannot close sendmail: $!";
    or
    `echo "$msgbody"|mail -s "$subject!" user\@somewhere.com`;
Re: capture output and email on linux
by cmeyer (Pilgrim) on Jun 09, 2005 at 17:25 UTC

    Try:

      perldoc -q capture output of command

    You may find something helpful here. :)

    -Colin.

    WHITEPAGES.COM | INC

Re: capture output and email on linux
by graff (Chancellor) on Jun 09, 2005 at 17:42 UTC
    Assuming that you trust the contents of the "netapps" file (who has write permission on that?), and you don't have any worries about using rsh instead of ssh (which is what I would use) -- and assuming that the local host is a *n*x box of some sort with its own mailer daemon -- here is a simple way:
    my $recipient = "somebody@somewhere.dom"; while (<IN>) { chomp; system( "rsh $_ snapmirror status | mail $recipient" ); }
    The default behavior of rsh (or ssh) is to attach the remote process's STDOUT to the local process STDOUT (in this case, the STDOUT of the shell being spawned by "system()"), so you can pipe the output of the rsh'ed command to some other (local) process on the same command line.
      I think you mean single ticks:
      my $recipient = 'somebody@somewhere.com';
      Perl will gripe about double quotes in that instance -
      cheers -

      Jeffery
Re: capture output and email on linux
by NiJo (Friar) on Jun 10, 2005 at 17:40 UTC
    xargs -n 1 "rsh {} snapmirror status | mail your@address " < netapps

    Perl is not the hammer for every nail. It mails from the remote hosts. Quoting might be wrong. But you get the idea.