CMD batch script to query DNS settings via BIND

A Simple CMD script for checking DNS settings in bulk can be useful for automation, to eliminate manual checking time or for reporting, to generate reports in a fast and time saving manner.

Here is a sample script which can check the MX records for a given set of domains:

@echo off
setlocal enabledelayedexpansion

rem Set input and output file paths
set "inputFile=domains.txt"
set "outputFile=mx_results.txt"

rem Clear the output file
echo. > %outputFile%

rem Loop through each line in the input file
for /f "delims=" %%i in (%inputFile%) do (
   rem Set the default valueof the variable to "ERROR"
   set "result=ERROR"
   rem Perform the MX lookup via "dig" and save the result to a variable
   for /f "delims=" %%j in ('dig +short mx %%i') do (
      set "result=%%j"
   )

   rem Append the result to the output file
   echo !result! >> %outputFile%
)

This script assumes that the input file “domains.txt” is in the same directory as the script, and it will save the results to a file named “mx_results.txt” in the same directory. The script uses the dig command, developed by BIND, to perform a MX query for each domain listed in the “domains.txt” file and saves the result in variable. Finally it will append the variable to the output file.

You should be able to run this script on Windows Command Prompt by navigating to the directory containing the script and running the command:

cmd /c script.cmd

Install BIND to use dig under Windows

1. Download the latest native Windows BIND version (BIND 9.16.37).

2. Extract the zip file and run “BINDInstall.exe” as administrator from the extracted folder.

3. Set the Target Directory, Service Account Name, Service Account Password and press “Install”.

BIND 9 Installer
BIND 9 installation

4. The next prompt will ask us to install Microsoft Visual C++ 2017. Accept the terms and conditions and press “Install”.

Microsoft Visual C++ 2017 Installation
Microsoft Visual C++ 2017 installation

5. If you see an error message “Setup Failed”, most likely this is caused by an already installed Microsoft Visual C++ 2017 version.

Microsoft Visual C++ 2017 Error
Microsoft Visual C++ 2017 error: 0x80070666

6. Finally, you should see a “BIND installation completed successfully” message.

BIND 9 Installation successful
BIND 9 installation completed

7. Add the BIND path to the system variables:
Press “Win + R“, type “sysdm.cpl“, navigate to “Advanced” > “Environment Variables…” . Under “System variables” select “Path“, press “Edit…“, click “New“, add “C:\Program Files\ISC BIND 9\bin” as path (the Target Directory from before) and click “OK“.

Edit System Variable BIND
Edit environment variable

8. Let’s verify the successful setup via CMD and the following command:
dig +short mx gmail.com

CMD dig Gmail
CMD DIG output for Gmail MX records

Useful DIG commands

dig example.com – This will perform a DNS lookup for the domain name “example.com” and retrieves only the A record (IP address) for the specified domain name.

dig example.com any – This will perform a DNS lookup for the domain name “example.com” and retrieves all available DNS records for the specified domain name, including A records (IP addresses), MX records (mail servers), NS records (name servers), CNAME records (aliases), TXT records (text records), and any other available records.

dig +short example.com – This will perform a DNS lookup for the domain name “example.com” and display only the IP address.

dig +short -x 8.8.8.8 – This will perform a reverse DNS lookup for the IP address “8.8.8.8” and display only the domain name associated with it.

dig [record type] example.com – This will perform a DNS lookup for the domain name “example.com”, for a specific record type.

Some common record types include:
A (Address) record: returns the IP address of the domain name
MX (Mail exchange) record: returns the mail server(s) that handle email for the domain
NS (Name server) record: returns the authoritative name server(s) for the domain
CNAME (Canonical name) record: returns the canonical name of an alias
TXT (Text) record: returns any text associated with the domain name, such as SPF records, DKIM records, or other types of text records.

dig +short MX example.com – This will perform a DNS lookup for the mail exchange (MX) records of the domain name “example.com” and display the results.

dig NS example.com – This will perform a DNS lookup for the name server (NS) records of the domain name “example.com” and display the results.

dig +trace example.com – This will perform a DNS lookup for the domain name “example.com” and display the full path of the query from the root nameservers to the authoritative nameservers.

dig +timeout=5 example.com – This will perform a DNS lookup for the domain name “example.com” and set the query timeout to 5 seconds.

dig +qr example.com – This will perform a DNS lookup for the domain name “example.com” and display only the query and response section.

dig +edns=0 example.com – This will perform a DNS lookup for the domain name “example.com” and disable EDNS (Extension mechanisms for DNS) for the query.

dig @8.8.8.8 example.com – This will perform a DNS lookup for the domain name “example.com” using the nameserver at IP address 8.8.8.8.

dig +noall +answer example.com – This will perform a DNS lookup for the domain name “example.com” and display only the answer section of the response.

dig +nostats example.com – This will perform a DNS lookup for the domain name “example.com” and suppress the display of statistics about the query.

dig +notcp example.com – This will perform a DNS lookup for the domain name “example.com” and avoid using TCP for the query.

dig +noadditional example.com – This will perform a DNS lookup for the domain name “example.com” and suppress the display of additional section of the response.

asterix Written by:

Be First to Comment

Leave a Reply

Your email address will not be published. Required fields are marked *