Unix and Linux network configuration. Multiple network interfaces. Bridged NICs. High-availability network configurations.


Reviews of latest Unix and Linux software. Helpful tips for application support admins. Automating application support.


Disk partitioning, filesystems, directories, and files. Volume management, logical volumes, HA filesystems. Backups and disaster recovery.


Distributed server monitoring. Server performance and capacity planning. Monitoring applications, network status and user activity.

Commands & Shells

Cool Unix shell commands and options. Command-line tools and application. Things every Unix sysadmin needs to know.

Home » Commands & Shells

Gather MX Records for a List of Domains

Submitted by on October 26, 2016 – 3:45 pm

This is a simple one: get a list of MX records for the given domains and output into CSV file. Really, the only interesting part of this is the use of array to temporarily store output of the dig command. The array is later used to prepend the correct number of column headers to the CSV file. This can be particularly useful of the output is used to generate a database table.

IFS=$'\n'; a=($(for d in `grep -v ^# "${infile}" | grep .`; do mx="$(timeout 10 dig ${d} MX 2>/dev/null | \
grep "^${d}.*MX" | awk '{print $NF}' | sort -u | sed 's/\.$//g')"; if [ ! -z "${mx}" ]; then echo ${d} ${mx} | \
sed 's/ /,/g'; else echo ${d}; fi; done)); unset IFS
printf '%s\n' ${a[@]} | printf '%s\n' ${a[@]} | (echo "DOMAIN,$(for i in $(seq 1 $(printf '%s\n' ${a[@]} | \
awk -F, '{if (NF > max) {max = NF}} END{print max}')); do echo -n "MX_RECORD_${i},"; done | sed 's/,$//g')" && cat) | \
tee "${outfile}"


Print Friendly, PDF & Email

Leave a Reply