#!/usr/bin/perl -wT # creator : Tomte # created : Don 07 Nov 2002 10:15:00 CET # version : $Id: mailshell.pl,v 1.7 2003/06/02 08:32:36 tom Exp $ use strict; # # config # # standard recipients my @mail_to = qw(regner@dievision.de); # send if execution succesful? my $send_on_success = 0; # pipe through my $mail_cmd = "/usr/sbin/sendmail -t"; # execute my $command = '/bin/true'; # Mail From: my $from = "mailshell.pl"; # # END config # # running -T $ENV{'PATH'} = ''; $ENV{'BASH_ENV'} = ''; $ENV{'ENV'} = ''; # # parameter "parsen" # untaint if ok # foreach my $i (0..$#ARGV) { # no source-routing, no fancy stuff push( @mail_to, $1) if ($ARGV[$i] =~ m/^-to$/ && $ARGV[$i+1] =~ m/^([a-z0-9.]+\@[a-z0-9.]+$)/i); # this should be secure enough, if you use this script, you should trust yourself :-) $command = $1 if ($ARGV[$i] =~ m/^-cmd$/ && $ARGV[$i+1] =~ m/^([:%'"\/\.a-z0-9 \-_]+)$/i); # from is localpart only, sendmail shall provide the domain $from = $1 if ($ARGV[$i] =~ m/^-from$/ && $ARGV[$i+1] =~ m/^([a-z0-9.]+)$/); # a better solution anybody? toggleOpt(\$send_on_success) if $ARGV[$i] =~ m/^-sos$/; } # stderr auf stdout mappen $command .= ' 2>&1'; # 'autoflush', couldn't possibly hurt!?! $| = 1; my $result = `$command`; $result =~ s/[$;'"]//g if $result; if ( (scalar(@mail_to)) > 0 && ((defined($result) && $result ne '') || $send_on_success) ){ my $recipients = join ',', @mail_to; open( MAIL, "|$mail_cmd"); select( MAIL); print "To: $recipients\n"; print "From: $from\n"; print "Subject: $command\n\n"; print "executed successful" if (!defined($result) || $result eq ''); print "Output:\n\n" . $result . "\n" if( $result); select(STDOUT); close(MAIL); } sub toggleOpt { my $to_toggle = shift; $$to_toggle = $$to_toggle == 0 ? 1 : 0; } __END__ =pod =head1 NAME mailshell.pl - send a mail if a programm produced output on stdout/stderr =head1 SYNOPSIS mailshell.pl [-sos] [-to e_mail]* [-from name] [-cmd what to run] =over =item B<-sos> send on success =item B<-to e_mail> C to address C =item B<-from name> local part of from-address =item B<-cmd cmd> the command to execute =back =head1 DESCRIPTION B if you and others (possibly colleagues) want to be notified via e-mail, if a command, e.g. run from cron, produced output on stdout I stderr. Using the C<-sos> switch you will be notified in any case after the command was executed. A C complient mailer is requiered! Basic configuration, the variables on top of the code, should be undertaken. =head1 EXAMPLES =over =item A crontab-entry C<<< */15 * * * * cd /home/tom/tools/bin && ./mailshell.pl -cmd './mycmd.sh' -from 'mycmd' -to name1@domain1 -to name2@domain1 > /dev/null 2>&1 >>> executes ./mycmd and notifies the default recipient as well as name1 and name2 if an output was generated from ./mycmd. =back =cut