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