· 8 years ago · Feb 20, 2018, 08:52 PM
1#!/bin/sh
2
3# OSX friendly version by jeff donovan
4#
5# Grep for a pattern through a Postfix mail log, collect the message ids into a temporary
6# file and then grep for all occurrences of the ID's in the maillog.
7# This is a very intensive operation since it requires 1+N greps through the entire log file,
8# where N is the number of unique ID's returned from the first grep.
9#
10# Usage sample:
11# ./grep-postfix-message-ids.sh @gmail.com
12# ./grep-posftix-message-ids.sh "from=<kenneth.kalmer"
13#
14
15if [ -z $1 ]; then
16 echo "Usage: `basename $0` pattern [/var/log/mail.log]"
17 echo
18 exit 1
19fi
20
21PATTERN=$1
22if [ -z $2 ]; then
23 MAILLOG=/var/log/mail.log
24else
25 MAILLOG=$2
26fi
27
28if [ ! -f $MAILLOG ]; then
29 echo "Maillog $MAILLOG doesn't exist"
30 echo
31 exit 1
32fi
33touch /var/log/tempfile
34TEMPFILE=/var/log/tempfile
35egrep "$PATTERN" $MAILLOG | awk '{print $6}' | tr -d : | uniq > $TEMPFILE
36for message_id in `cat $TEMPFILE`
37do
38 grep $message_id $MAILLOG
39done
40
41rm -f $TEMPFILE