Monday morning shell script

The University provides Exchange accounts for our students. However, our students like to use an “@law.emory.edu” address to advertise their affiliation with the law school. We provide this service through a mail-forwarding service that forwards mail addressed to the @law.emory.edu addresses to the student’s “real” account at the University. After students graduate, the University turns off their Exchange accounts. As a result, mail to the “@law.emory.edu” accounts generates errors if delivery is attempted to the University Exchange account. It would be nice to create an automated way to purge the no longer working aliases from the /etc/aliases file. Here’s a first cut at doing that.

This bash script updates Postfix’s /etc/aliases file when we detect that the University no longer has an account for the person in question. It’s designed to be run once a day against the log file generated by pflogsumm.

CAUTION: This probably goes without saying but use this as a model for your own experiments. Be careful. Sed is being used non-interactively here and it will modify your aliases file as a result of running this script. It will also run newaliases. So, if you nuke your aliases file and then run newaliases, you will lose all of your email aliases and email will bounce. Your phone will ring.

If you have feedback or questions about the script, let me know in the comments.

#!/bin/bash

###############################################################
# Basic script to parse output from Postfix logfile
# If user is no longer available in Emory exchange,
# comment out related line in /etc/aliases and run newaliases.
#
# Date: Mon Sep 19 09:19:51 EDT 2011
# Author: Ben Chapman
# Released into the public domain.
#
# Suggestions for improvement welcomed. There's an error in
# that the regex does not ignore previously commented lines.
#
###############################################################

LOGFILE=/var/www/html/mailgraph/pflogsumm.txt
TMPFILE=$(mktemp)
grep "User unknown (" $LOGFILE | sed "s/.*<(.*)>.*/1/" >$TMPFILE
cp -f /etc/aliases /etc/aliases.good
for x in $(cat $TMPFILE)
        do
                sed -i "s/(.*:$x)/#1/" /etc/aliases
        done
rm -f $TMPFILE
newaliases
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s