Dig in batch mode

The Internet Software Consortium’s (ISC) dig is the recommended tool for querying Domain Name System (DNS) servers. Recently I needed to check a number of DNS entries tied to the same IP address (the typical A record with associated CNAME records). After leafing through the manual page and some examples I found on the Internet, I came up with this solution.

The key to efficiently handling queries of multiple host names in one shot with dig is making maximum use of the different options and flags available in the utility.

According to the man page, dig will process a list of host names contained in a plain text file having one host name per line.

Take a file like this (call it hostnames.txt):


(be sure there are no blank lines leading, trailing or in-between — these will return a messy dump of root server info)

Then run dig against it like dig -f hostnames.txt.

The output you’ll get from this will be pretty verbose. That’s dig. To strip that output down to the essentials (A and CNAME records), try this syntax:

dig -f hostnames.txt +noall +answer

The “+noall” flag toggles the display of full information off, “+answer” ensures that the answer section of each record is returned. The response you’ll get will be something like this:

example.com.      300 IN A
www.example.com.  300 IN CNAME example.com.
www2.example.com. 300 IN CNAME example.com.
blog.example.com. 300 IN CNAME example.com.
dev.example.com.  300 IN CNAME example.com.

Note for those with Windows workstations:

For a long time anyone who wanted to run dig on Windows needed to load up a bulky Unix emulation environment like Cygwin. Although I’m a big fan of Cygwin, there’s a better solution for those who just need dig and maybe a few other up-to-date DNS utilities. To get them all you have to do is go up and grab the latest BIND for Windows. Once downloaded, just extract the following files to their own folder (e.g. c:appsbind):


These are the bare minimum. You might also consider getting host.exe and other utiltiies, as well as the man pages in .html format or the full BIND Administrator’s Manual in pdf format.

After extracting the files, add the folder path to the Windows system environment so that you can execute the utilities from any command prompt.