#!/bin/bash # Name: find-recipients-files # Purpose: The find-recipients.pl Perl script checks a given sendmail maillog # file for a specified sender email address and returns a list of recipients # email addresses to which email was sent. This BASH script can be used to # search multiple files with the find-recipients.pl script using wildcard # characters, such as "?" and "*". The "?" means any single matching character # and "*" means any matching sequence of characters. E.g. one could search # for all maillog files in /tmp/maillog that have maillog as the first # part of the filename followed by an extension representing MMDDYY for # files for March 2006 by calling this script with the following command: # # ./find-recipients-files jdoe@example1.com '/tmp/maillog/maillog.03*06' # # Note: the second argument, when it includes a "?" or a "*", should have # a backslash before the "?" or "*" or the entire arugment should be # enclosed in single or double quotes as shown above. # -t can be used to specify an optional recipient email address # -s can be used to select only messages with a certain status, # e.g. Deferred or Sent. # # Example: # ./find-recipients -t you@example2.com -s Sent jdoe@example1.com /tmp/maillog # Written By: Jim Cameron # Created: Arpil 3, 2006 # Last Modified: December 27, 2008 # Version: 1.2 # Download URL: # http://support.moonpoint.com/downloads/computer_languages/bash/find-recipients-files function printMessageHeader { echo -e "Message recipients\n" printf "%-15s %-14s %-13s %s\n" "Time" "Message ID" "Status" "Recipient" echo -e "--------------------------------------------------------------------------\n" } function printUsage { echo "Usage: `basename $0` [-t recipient] [-s status] sender files" echo " " echo "Example: `basename $0` jdoe@example1.com /tmp/maillog/maillog.03\*06" echo " " echo "Searches for the sender's email address in all maillog files in" echo "/tmp/maillog for the month of March 2006. You will need to" echo "place a \"\\\" before any \"*\" or \"?\" or place the" echo "second argument within either single or double quotes." echo " " echo "-t can be used to specify an optional recipient email address" echo "-s can be used to select only messages with a certain status," echo " e.g. Deferred or Sent." } optlist="" while getopts "t:s:" options do case $options in t ) to=$OPTARG optlist="--to $to";; s ) status=$OPTARG optlist="$optlist --status $status";; \? ) printUsage exit 1;; * ) printUsage;; esac done # If no arguments appear on the command line, display usage information if [ $# -eq 0 ] then printUsage exit fi # If no "to" or "status" arguments are on the command line, then # there should be 2 arguments on the command line, one specifying # the sender and the other spcifying the maillog file(s) to be searched if [ -z $to ] && [ -z $status ] then if [ $# -ne 2 ] then printUsage exit 1 else sender=$1 logs=$2 fi # If only one has been specified, there should be 4 arguments on the # command line. elif [ -z $to ] || [ -z $status ] then if [ $# -ne 4 ] then printUsage exit 1 else sender=$3 logs=$4 fi else # Both have been specified, so there should be 6 arguments on the # command line if [ $# -ne 6 ] then printUsage exit 1 else sender=$5 logs=$6 fi fi # Display just the file names for those files where the sender's email # address occurs somewhere in the file. # Use command substitution to put the results of the grep search in "files". files=$(grep -l $sender $logs) printMessageHeader for file in $files; do ./find-recipients.pl --noheading $optlist $sender $file done