#!/bin/bash
# Monitor mx record
# This script will check the mx record of a domain name against a predefined value for a given list of domains.
# It will sort the mx records for the domain numerically by priority and use the lowest value.
# Make the file executable and copy it into /etc/cron.hourly
# It assumes that dig is installed
# To check the expected record for a domain e.g. google.com
# dig +short google.com MX | sort -n | head -n 1 | awk -F' ' '{print $2}' | tr '[:upper:]' '[:lower:]'
# live will send an email
LIVE=1; # yes=1, no=0;
# will send message to this email address
EMAIL=monitor@domain.com;
# MX_RECORDS is an associative array of domain as key and expected mx record as value. MX_RECORDS['domain']='expected_value';
unset MX_RECORDS; # empty array
declare -A MX_RECORDS; # declare associative array
# Add your own domains and expected mx records below below
# Zoho
MX_RECORDS['zoho.com']='smtpin.zoho.com.';
# Office365 / Exchange
MX_RECORDS['microsoft.com']='microsoft-com.mail.protection.outlook.com.';
# Gmail
MX_RECORDS['google.com']='aspmx.l.google.com.';
# Loop through associative array
# DOMAIN is the key
# ${MX_RECORDS[$DOMAIN]} is the value
for DOMAIN in "${!MX_RECORDS[@]}";
do
MX_RECORD=$(dig +short $DOMAIN MX | sort -n | head -n 1 | awk -F' ' '{print $2}' | tr '[:upper:]' '[:lower:]');
EXPECTED_MX_RECORD=${MX_RECORDS[$DOMAIN]};
# test {} = $EXPECTED_MX_RECORD && "match" || "no macth"
if [ $LIVE -eq 0 ]; then
echo $MX_RECORD | xargs -I{} test {} = $EXPECTED_MX_RECORD && : || echo "Subject: MX record for $DOMAIN, $MX_RECORD does not match $EXPECTED_MX_RECORD";
elif [ $LIVE -eq 1 ]; then
echo $MX_RECORD | xargs -I{} test {} = $EXPECTED_MX_RECORD && : || echo "Subject: MX record for $DOMAIN, $MX_RECORD does not match $EXPECTED_MX_RECORD" | tee /dev/stderr | sendmail $EMAIL;
else
: # do nothing
fi
done
{"html5":"htmlmixed","css":"css","javascript":"javascript","php":"php","python":"python","ruby":"ruby","lua":"text\/x-lua","bash":"text\/x-sh","go":"go","c":"text\/x-csrc","cpp":"text\/x-c++src","diff":"diff","latex":"stex","sql":"sql","xml":"xml","apl":"apl","asterisk":"asterisk","c_loadrunner":"text\/x-csrc","c_mac":"text\/x-csrc","coffeescript":"text\/x-coffeescript","csharp":"text\/x-csharp","d":"d","ecmascript":"javascript","erlang":"erlang","groovy":"text\/x-groovy","haskell":"text\/x-haskell","haxe":"text\/x-haxe","html4strict":"htmlmixed","java":"text\/x-java","java5":"text\/x-java","jquery":"javascript","mirc":"mirc","mysql":"sql","ocaml":"text\/x-ocaml","pascal":"text\/x-pascal","perl":"perl","perl6":"perl","plsql":"sql","properties":"text\/x-properties","q":"text\/x-q","scala":"scala","scheme":"text\/x-scheme","tcl":"text\/x-tcl","vb":"text\/x-vb","verilog":"text\/x-verilog","yaml":"text\/x-yaml","z80":"text\/x-z80"}