dig command bulk/batch lookups to read hostnames from a text file

Fig.03: Doing a bulk/batch dns lookups using dig command

Fig.03: Doing a bulk/batch dns lookups using dig command

The syntax is:

dig -f fileName.txt

The -f option makes dig operate in batch mode by reading a list of lookup requests to process from the file filename. The file contains a number of queries, one per line. Each entry in the file should be organized in the same way they would be presented as queries to dig using the command-line interface.

Create a text file called dns-lookups.txt

$ cat dns-lookups.txt
cyberciti.biz ANY
www.reddit.com A
slashdot.org A
redhat.com MX

Force dig to operate in batch mode

Run it as follows:
$ dig -f dns-lookups.txt
Sample outputs:

; <<>> DiG 9.8.3-P1 <<>> cyberciti.biz ANY
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 46634
;; flags: qr rd ra; QUERY: 1, ANSWER: 12, AUTHORITY: 0, ADDITIONAL: 0

;; QUESTION SECTION:
;cyberciti.biz.			IN	ANY

;; ANSWER SECTION:
cyberciti.biz.		299	IN	A	75.126.153.206
cyberciti.biz.		21599	IN	NS	ns-1075.awsdns-06.org.
cyberciti.biz.		21599	IN	NS	ns-1947.awsdns-51.co.uk.
cyberciti.biz.		21599	IN	NS	ns-243.awsdns-30.com.
cyberciti.biz.		21599	IN	NS	ns-866.awsdns-44.net.
cyberciti.biz.		899	IN	SOA	ns-1075.awsdns-06.org. awsdns-hostmaster.amazon.com. 1 7200 900 1209600 86400
cyberciti.biz.		3599	IN	MX	1 aspmx.l.google.com.
cyberciti.biz.		3599	IN	MX	10 aspmx2.googlemail.com.
cyberciti.biz.		3599	IN	MX	10 aspmx3.googlemail.com.
cyberciti.biz.		3599	IN	MX	5 alt1.aspmx.l.google.com.
cyberciti.biz.		3599	IN	MX	5 alt2.aspmx.l.google.com.
cyberciti.biz.		21599	IN	AAAA	2607:f0d0:1002:51::4

;; Query time: 67 msec
;; SERVER: 8.8.8.8#53(8.8.8.8)
;; WHEN: Mon Jan 13 18:36:44 2014
;; MSG SIZE  rcvd: 406

; <<>> DiG 9.8.3-P1 <<>> www.reddit.com A
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 51891
;; flags: qr rd ra; QUERY: 1, ANSWER: 4, AUTHORITY: 0, ADDITIONAL: 0

;; QUESTION SECTION:
;www.reddit.com.			IN	A

;; ANSWER SECTION:
www.reddit.com.		3429	IN	CNAME	reddit.com.edgesuite.net.
reddit.com.edgesuite.net. 10611	IN	CNAME	a659.b.akamai.net.
a659.b.akamai.net.	11	IN	A	58.26.1.64
a659.b.akamai.net.	11	IN	A	58.26.1.35

;; Query time: 28 msec
;; SERVER: 8.8.8.8#53(8.8.8.8)
;; WHEN: Mon Jan 13 18:36:44 2014
;; MSG SIZE  rcvd: 130

; <<>> DiG 9.8.3-P1 <<>> slashdot.org A
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 23905
;; flags: qr rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 0

;; QUESTION SECTION:
;slashdot.org.			IN	A

;; ANSWER SECTION:
slashdot.org.		179	IN	A	216.34.181.45

;; Query time: 28 msec
;; SERVER: 8.8.8.8#53(8.8.8.8)
;; WHEN: Mon Jan 13 18:36:44 2014
;; MSG SIZE  rcvd: 46

; <<>> DiG 9.8.3-P1 <<>> redhat.com MX
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 63662
;; flags: qr rd ra; QUERY: 1, ANSWER: 2, AUTHORITY: 0, ADDITIONAL: 0

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

;; ANSWER SECTION:
redhat.com.		387	IN	MX	10 mx2.redhat.com.
redhat.com.		387	IN	MX	5 mx1.redhat.com.

;; Query time: 62 msec
;; SERVER: 8.8.8.8#53(8.8.8.8)
;; WHEN: Mon Jan 13 18:36:44 2014
;; MSG SIZE  rcvd: 68

Examples and usage: Linux and Unix dig Command Examples

4 comment

  1. dig -f filename.txt +noall +answer

    will give you the ip address 1 per line incase the output wants to be used programatically

  2. I am trying to use the following command on a logfile with lines of IP addresses only but the output is the ip address. I am trying to ONLY get the hostname out as is with:

    dig +short -x 192.168.10.5
    output is : servername.domain.local.

    That’s what I want… Now if I run:

    dig -x -f iplist.log (I get lots of output, but I just want fqdn)

    ok, so then I try:

    dig -x -f iplist.log +short (I get no output)
    dig -x -f iplist.log +noall +short (I get no output)

    What am I doing wrong? Any help is greatly appreciated.

    Thanks!

    Richard

    1. Richard, It seems there’s no way to bulk reverse lookup IP using dig. If there’s one, I am
      also looking for it.

      I would do

      cat list | xargs -Ih host h
      or
      cat list | xargs -Ih dig +noall +answer -x h

      1. Create a file called ip.txt as follows:

        +short -x 74.86.144.194  
        +short -x 216.34.181.45
        +short -x 216.58.197.78
        +short -x 216.58.199.197
        +short -x 54.230.190.174

        Run it as follows:

        dig -f ip.txt 
        outputs:
        c2.90.564a.ip4.static.sl-reverse.com.
        slashdot.org.
        maa03s21-in-f14.1e100.net.
        sin04s09-in-f5.1e100.net.
        server-54-230-190-174.maa3.r.cloudfront.net.
        

    Have a question? Post it on our forum!