#! /opt/CSCOpx/bin/perl # # Copyright (c) 1996-2000, 2002 Cisco Systems, Inc. # All rights reserved # # Name: sampleActionScript.pl # # Module: CSCOsa # # Description: Sample action script that emails a syslog message # to the specified email ids. # # Usage: sampleEmailScript.pl -email_ids , # -subject [] optional # -from [] # -sender [] # -smtp [], # -text_message [] # # If unable to send mail, the error is printed out to Standard Error. # # Last modified: 6/22/98 # # initialize enviroment, need this for Perl scripts # this is a must for NT use CRM; # parse the command line arguments my %command_line_args = parse_args(@ARGV); # setup mailing information # mail ids could be a comma seperated list my $email_ids = $command_line_args{EMAIL_IDS}; my $text_message = $command_line_args{TEXT_MESSAGE}; # these arguments are optional my $subject = $command_line_args{SUBJECT}; my $from = $command_line_args{FROM}; my $sender = $command_line_args{SENDER}; my $smtp_server = $command_line_args{SMTP}; # if no email id is found print the usage if (!$email_ids || !$text_message) { print <, -subject [] optional -from [] -sender [] -smtp [], -text_message [] EOT ; exit(0); } # make sure there is a mail id if (!$email_ids) { printf STDERR "ERROR: No EMail Ids specified.\n"; exit(0); } # need to put the message text into an array of lines $text_message = 'empty message' if (!$text_message); # save the text into a file, before mailing it. For NT, using # pipes with Blat.exe is not reliable $temp_file_name = time(); # Fix for CSCdr70603. Just taking the time used to cause two mails to step on each other's file # This is possible as two automated actions can be kicked off at the same time. In perl the time # function gives resolution only to the second level and so it is possible for two actions to get ready # to send mail within the same second. # To fix this , using the time function I generate a random number and use this number as the file name. $temp_file_name = rand($temp_file_name ^ ($$ + ($$ << 15)) ); $temp_file_name = $ENV{PX_TMPDIR} . "/tempEmail." . $temp_file_name; # open the file for reading unless (open(TEMP_FILE, ">$temp_file_name")) { printf STDERR "ERROR: Unable to open temporary mail file, $temp_file_name"; exit(0); } # write the email message to the file print TEMP_FILE "Dear CW2K User, \n\n"; print TEMP_FILE " This email is generated by the sampleEmailScript.pl program.\n"; print TEMP_FILE "The syslog message that was configured for this automated action is:\n\n"; print TEMP_FILE "$text_message\n"; close (TEMP_FILE); my $ret = send_mail(-email_ids=>$email_ids ,-subject=>$subject ,-from=>$from ,-sender=>$sender ,-smtp=>$smtp_server ,-filename=>$temp_file_name ); unlink($temp_file_name); if (defined $ret) { printf STDERR "ERROR: Unable to send mail.\n $ret\n"; } exit(0); #--------------------------------------------------------------------------- # Name: send_mail # Synopsis: undef/define send_mail(-email_ids=> , # -subject=> [] optional # -from=> [] # -sender=> [] # -mail_prog=> [] # -smtp=> [], # -filename=> [], # -text_message=>[] # # Description: sends an email to the give list of email ids # Returns: undef if successful, otherwise a error string #--------------------------------------------------------------------------- sub send_mail { my (%param) = parse_param(@_); # get all the parameters for the calling functions my $email_addrs = $param{EMAIL_IDS}; # these parameters are optional my $filename = $param{FILENAME}; my $subject = $param{SUBJECT}; my $from = $param{FROM}; my $mail_program = $param{MAIL_PROG}; my $text_message = $param{TEXT_MESSAGE}; my $sender = $param{SENDER}; my $smtp_server = $param{SMTP}; my $fromname; # assign the default mail program and subject,from, if one is not supplied if (!$mail_program) { $mail_program = '/usr/lib/sendmail'; $mail_program = "$ENV{'PX_BINDIR'}/blat" if $CRM::CRM_OS eq 'WIN'; } $subject = 'SyslogAnalyzer Action Script' if (!$subject); # use the default $from = 'root' if (!$from); if ($sender) { $fromname = "SyslogAnalyzer on behalf of $sender"; } else { $fromname = "SyslogAnalyzer"; } # open a pipe to the mail program my $openMail; if ($CRM::CRM_OS eq 'WIN') { if (!(defined $text_message)) { my $rc = system "$mail_program $filename -t $email_addrs -s \"$subject\" -f $from -i \"$fromname\" -server \"$smtp_server\""; if ($rc != 0) { return "Unable send mail - Mail Program: $mail_program To: $email_addrs Using SMTP Server: $smtp_server"; } return undef; } $openMail = open(MAIL_PROG, "|$mail_program - -t $email_addrs -s \"$subject\" -f $from -i \"$fromname\" -server \"$smtp_server\""); } else { $openMail = open(MAIL_PROG, "|$mail_program -t") ; } if (!$openMail) { return "Unable open pipe to Mail Program: $mail_program"; } # if a message array is provided, then we'll just send that instead if (!(defined $text_message)) { if (!open(RESULT_FILE, "$filename")) { return "Unable to open file to mail: $filename"; } } # print the headers need for sendmail print MAIL_PROG "To: $email_addrs\n" if ($CRM::CRM_OS ne 'WIN'); print MAIL_PROG "Sender: $from\n" if ($CRM::CRM_OS ne 'WIN'); print MAIL_PROG "From: $fromname\n" if ($CRM::CRM_OS ne 'WIN'); print MAIL_PROG "Subject: $subject\n\n" if ($CRM::CRM_OS ne 'WIN'); if (!(defined $text_message)) { # print all the contents of the given file while () { chop; print MAIL_PROG "$_\n"; } } else { my $i; for $i (0 .. $#$text_message) { print MAIL_PROG "$$text_message[$i] \n"; } } # we are done, close the pipe and file close (MAIL_PROG); close (RESULT_FILE); return undef; } #--------------------------------------------------------------------------- # Name: parse_args # Synopsis: array parse_args( 'value', # 'value2' #--------------------------------------------------------------------------- sub parse_args { my @param = @_; my %ret = (); for ($i=0;$i<@param;$i+=1) { # only take the arguments that begin with a - character if ($param[$i] =~/^-/) { $param[$i]=~s/^\-//; # get rid of initial - if present $param[$i]=~tr/a-z/A-Z/; # parameters are upper case $j = $i + 1; if ($CRM::CRM_OS ne 'WIN' && $param[$j] =~/^"/) { # compact the text between "'s into 1 argument $temp = $param[$j]; $temp =~s/^"//; # Check if the same string ends with " (CSCdr10072) if ($temp =~/"$/) { $temp =~s/"$//; } else { for ($j = $j+1; $j <@param; $j++) { if ($param[$j] =~/"$/) { $param[$j] =~s/"$//; $temp .= " " . $param[$j]; last; } else { $temp .= " " . $param[$j]; } } } # restore the arguments $ret{$param[$i]} = $temp; $i = $j; } else { $ret{$param[$i]} = $param[$i+1]; } } } return(%ret); } #--------------------------------------------------------------------------- # Name: parse_param # Synopsis: array parse_param('value', # -param2=>'value2', and so on) # Description: parses the input and build an array without the '-'s and # capitalize all the params # Returns: an array, in this case: 'value', # =>'value2' #--------------------------------------------------------------------------- sub parse_param { my @param = @_; my $i; for ($i=0;$i<@param;$i+=2) { $param[$i]=~s/^\-//; # get rid of initial - if present $param[$i]=~tr/a-z/A-Z/; # parameters are upper case } return(@param); }