Digcheck SH script. Only want to see MX output.

I want to filter out all the junk and only see the Domain name and Mx records. Can't quite figure out how to get this output.

This is what i'm using to get the output.

dig mx -f domains.txt > mx.txt

This is what i see when i open the text file

; <<>> DiG 9.2.4 <<>> jjenkinsonline.com
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 2550
;; flags: qr rd ra; QUERY: 1, ANSWER: 2, AUTHORITY: 2, ADDITIONAL: 0

;; QUESTION SECTION:
;jjenkinsonline.com.		IN	MX

;; ANSWER SECTION:
jjenkinsonline.com.	3593	IN	MX	10 mailstore1.secureserver.net.
jjenkinsonline.com.	3593	IN	MX	0 smtp.secureserver.net.

;; AUTHORITY SECTION:
jjenkinsonline.com.	3581	IN	NS	ns22.domaincontrol.com.
jjenkinsonline.com.	3581	IN	NS	ns21.domaincontrol.com.

;; Query time: 0 msec
;; SERVER: 172.24.36.46#53(172.24.36.46)
;; WHEN: Tue Apr 14 16:16:26 2009
;; MSG SIZE  rcvd: 152

; <<>> DiG 9.2.4 <<>> 4miles.com
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 11463
;; flags: qr rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 13, ADDITIONAL: 1

;; QUESTION SECTION:
;4miles.com.			IN	MX

;; ANSWER SECTION:
4miles.com.		3593	IN	MX	10 mail.4miles.com.

;; AUTHORITY SECTION:
com.			172781	IN	NS	J.GTLD-SERVERS.NET.
com.			172781	IN	NS	K.GTLD-SERVERS.NET.
com.			172781	IN	NS	L.GTLD-SERVERS.NET.
com.			172781	IN	NS	M.GTLD-SERVERS.NET.
com.			172781	IN	NS	A.GTLD-SERVERS.NET.
com.			172781	IN	NS	B.GTLD-SERVERS.NET.
com.			172781	IN	NS	C.GTLD-SERVERS.NET.
com.			172781	IN	NS	D.GTLD-SERVERS.NET.
com.			172781	IN	NS	E.GTLD-SERVERS.NET.
com.			172781	IN	NS	F.GTLD-SERVERS.NET.
com.			172781	IN	NS	G.GTLD-SERVERS.NET.
com.			172781	IN	NS	H.GTLD-SERVERS.NET.
com.			172781	IN	NS	I.GTLD-SERVERS.NET.

;; ADDITIONAL SECTION:
mail.4miles.com.	3593	IN	A	64.56.35.138

;; Query time: 0 msec
;; SERVER: 172.24.36.46#53(172.24.36.46)
;; WHEN: Tue Apr 14 16:16:26 2009
;; MSG SIZE  rcvd: 289

I would first suggest looking at the dig command and it's parameters to see if it will give the output you require via switches/parameters.

Else, something like this to grab A and MX records removing comments and blanks:

swapping [TABSPACE] for [ ] with a tab and a space in it.

or something a little more condensed like...


  1. ^; ↩︎