mailertable test script

The following is a script I wrote to test mailertable entries for SMTP connectivity, as our organization uses special mail routing for certain clients.

The script will work if mailertable values are MX records, A records, or IP addresses; the script uses netcat for the SMTP connectivity test.

#!/bin/sh

NSLOOKUP=/usr/sbin/nslookup
NC=/usr/local/bin/nc

# Test hostnames in mailertable
for MAILERTABLE_ENTRY in `awk -F: '{print $2}' /etc/mail/mailertable | egrep '^[a-zA-Z]+' | sort | uniq`
do
   echo "Testing $MAILERTABLE_ENTRY..."
   $NSLOOKUP -type=mx $MAILERTABLE_ENTRY | grep "mail exchanger =" > /dev/null 2>&1
   if [ $? -eq 1 ] ; then
      # The mailertable entry does not have an MX record
      $NC -z -w 5 $MAILERTABLE_ENTRY 25
      [ $? -eq 1 ] && echo "$MAILERTABLE_ENTRY SMTP connectivity test failed."
   else
      # The mailertable entry is an MX record; test connectivity to all hosts in MX record
      for HOST in `$NSLOOKUP -type=mx $MAILERTABLE_ENTRY | grep "mail exchanger =" | awk '{print $NF}' | sort | uniq`
      do
         $NC -z -w 5 $HOST 25
         [ $? -eq 1 ] && echo "$HOST SMTP connectivity test failed."
      done
   fi
done

# Test IP addresses in mailertable
for MAILERTABLE_ENTRY in `awk -F: '{print $2}' /etc/mail/mailertable | egrep '^\[[1-9]+' | sort | uniq | tr -d "\[\]"`
do
   echo "Testing $MAILERTABLE_ENTRY..."
   $NC -z -w 5 $MAILERTABLE_ENTRY 25
   [ $? -eq 1 ] && echo "$MAILERTABLE_ENTRY SMTP connectivity test failed."
done

Back to brandonhutchinson.com.
Last modified: 11/20/2003