Facebook
From andreb, 3 Years ago, written in Bash.
Embed
Download Paste or View Raw
Hits: 97
  1. #!/bin/bash
  2.  
  3. # Monitor mx record
  4. # This script will check the mx record of a domain name against a predefined value for a given list of domains.
  5. # It will sort the mx records for the domain numerically by priority and use the lowest value.
  6. # Make the file executable and copy it into /etc/cron.hourly
  7. # It assumes that dig is installed
  8.  
  9. # To check the expected record for a domain e.g. google.com
  10. # dig +short google.com MX | sort -n | head -n 1 | awk -F' ' '{print $2}' | tr '[:upper:]' '[:lower:]'
  11.  
  12. # live will send an email
  13. LIVE=1; # yes=1, no=0;
  14.  
  15. # will send message to this email address
  16. EMAIL=monitor@domain.com;
  17.  
  18. # MX_RECORDS is an associative array of domain as key and expected mx record as value. MX_RECORDS['domain']='expected_value';
  19. unset MX_RECORDS; # empty array
  20. declare -A MX_RECORDS; # declare associative array
  21.  
  22. # Add your own domains and expected mx records below below
  23.  
  24. # Zoho
  25. MX_RECORDS['zoho.com']='smtpin.zoho.com.';
  26.  
  27. # Office365 / Exchange
  28. MX_RECORDS['microsoft.com']='microsoft-com.mail.protection.outlook.com.';
  29.  
  30. # Gmail
  31. MX_RECORDS['google.com']='aspmx.l.google.com.';
  32.  
  33. # Loop through associative array
  34. # DOMAIN is the key
  35. # ${MX_RECORDS[$DOMAIN]} is the value
  36. for DOMAIN in "${!MX_RECORDS[@]}";
  37. do
  38.  
  39.    MX_RECORD=$(dig +short $DOMAIN MX | sort -n | head -n 1 | awk -F' ' '{print $2}' | tr '[:upper:]' '[:lower:]');
  40.    EXPECTED_MX_RECORD=${MX_RECORDS[$DOMAIN]};
  41.  
  42.    # test {} = $EXPECTED_MX_RECORD && "match" || "no macth"
  43.    if [ $LIVE -eq 0 ]; then
  44.      echo $MX_RECORD | xargs -I{} test {} = $EXPECTED_MX_RECORD && : || echo "Subject: MX record for $DOMAIN, $MX_RECORD does not match $EXPECTED_MX_RECORD";
  45.    elif [ $LIVE -eq 1 ]; then
  46.      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;
  47.    else
  48.      : # do nothing
  49.    fi
  50.  
  51. done
  52.