1

I've got a txt of a bunch of domains I need to run dig +short mx on.

I've got a script set to run the command, print it to a results.txt with:

./dig_domain_mx.sh > results.txt

Downside is, I need to be able to know which domains relate to each result, so my solution is to print the current line being read by the script, then print the dig output for the line, then add a linebreak.

I can't seem to find anything alluding to this being possible with dig commands alone, so how would I go about this within my Bash script?


Current Bash script, nothing special:

#!/bin/bash
dig -f domains.txt +short mx
3

To avoid running one dig and read per line of the file, you could do:

dig -f domains.txt mx +noall +answer

Which would give an output like:

stackexchange.com.      300     IN      MX      5 alt1.aspmx.l.google.com.
stackexchange.com.      300     IN      MX      1 aspmx.l.google.com.
stackexchange.com.      300     IN      MX      10 alt3.aspmx.l.google.com.
stackexchange.com.      300     IN      MX      10 alt4.aspmx.l.google.com.
stackexchange.com.      300     IN      MX      5 alt2.aspmx.l.google.com.
google.com.             600     IN      MX      10 aspmx.l.google.com.
google.com.             600     IN      MX      30 alt2.aspmx.l.google.com.
google.com.             600     IN      MX      20 alt1.aspmx.l.google.com.
google.com.             600     IN      MX      40 alt3.aspmx.l.google.com.
google.com.             600     IN      MX      50 alt4.aspmx.l.google.com.

You can pipe to awk '{print $1,$5,$6}' to remove the 300 IN MX.

An alternative to the while read loop could be xargs:

$ xargs -tn1 dig mx +short < domains.txt
dig mx +short stackexchange.com
1 aspmx.l.google.com.
10 alt3.aspmx.l.google.com.
10 alt4.aspmx.l.google.com.
5 alt2.aspmx.l.google.com.
5 alt1.aspmx.l.google.com.
dig mx +short google.com
20 alt1.aspmx.l.google.com.
30 alt2.aspmx.l.google.com.
40 alt3.aspmx.l.google.com.
50 alt4.aspmx.l.google.com.
10 aspmx.l.google.com.
  • Cool thanks! Ironically enough though, I scrapped my answer too; did a 180 and just switched to Java instead... – WorseDoughnut Apr 28 '16 at 18:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.