KOMPX.COM or COMPMISCELLANEA.COM   

Getting the name servers of a domain under Windows

Getting the name servers of a domain by means of NSLOOKUP command and other CMD tools under Microsoft Windows.

Just nslookup -type=ns domain.tld gives "non-authoritative answer". That is, info rather cached somewhere. It may happen to be outdated at the time of request, so it is better to reach a name server responsible for giving answers about a given domain name.

Hoping that info given by nslookup -type=ns domain.tld is not outrageously outdated, the next step is to query each name server from the list it gives. They are supposed to be authoritative for a given domain name: set as such by a domain name registrar, when the domain name was registered. The lists of name servers obtained from them are merged and duplicate entries removed.

Two variants:

  1. Variant #1
  2. Variant #2

Variant #1

Getting the name servers of a domain under Windows.

Code for .BAT file:


@ECHO OFF
SETLOCAL EnableDelayedExpansion

:: Setting domain name to query
SET "DOMAIN=kompx.com"

:: Creating and setting the output file
TYPE NUL > nameservers.txt
SET "OUTFILE=nameservers.txt"

:: Creating a temporary file for interim storage
SET "TMP=%TEMP%\FILES_%RANDOM%.TMP"

:: Getting a list of name servers / IP addresses
FOR /F "TOKENS=2,* DELIMS=^=" %%G IN ('NSLOOKUP -type^=ns %DOMAIN% ^| FINDSTR /C:"nameserver"') DO (
	FOR /F "TOKENS=2,* DELIMS=^=" %%H IN ('NSLOOKUP -type^=ns %DOMAIN% %%G ^| FINDSTR /C:"nameserver"') DO (
		ECHO %%H >> "%TMP%"
	)
)

:: Removing duplicate lines
FOR /F "USEBACKQ DELIMS=" %%I IN ("%TMP%") DO (
	:: Storing each line in a variable
	SET "LINE=%%I"
	:: Clearing a flag, tracking whether the line already exists
	SET "FOUND="
	:: Comparing each line to the current input line and setting the flag to 1, if a match is found
	FOR /F "USEBACKQ DELIMS=" %%J IN ("%OUTFILE%") DO (
		IF "!LINE!"=="%%J" SET "FOUND=1"
    )
	:: Appending the line, if found to be unique, to the output file
	IF NOT DEFINED FOUND (
		>> "%OUTFILE%" ECHO(!LINE!)
    )
)

:: Clearing temporary file for the next use
TYPE NUL > %TMP%

:: Removing leading and trailing spaces
FOR /F "USEBACKQ DELIMS=" %%L IN ("%OUTFILE%") DO (
	SET "NOSPACES=%%L"
	:: Removing leading spaces
	FOR /F "TOKENS=* DELIMS= " %%M IN ("!NOSPACES!") DO (
		SET "NOSPACES=%%M"
	)
	:: Removing trailing spaces by calling a subroutine
	CALL :REMTRAIL
	:: Writing cleaned lines to the temporary file
	ECHO !NOSPACES!>>"%TMP%"
)
:: Subroutine to remove trailing spaces
:REMTRAIL
IF "!NOSPACES:~-1!"==" " (
	SET "NOSPACES=!NOSPACES:~0,-1!"
	GOTO REMTRAIL
)

:: Sorting the contents of the temporary file alphabetically
SORT "%TMP%" /O "%TMP%"

:: Removing trailing blank line from the temporary file and writing the result to the output file
SET "FIRST=1"
> "%OUTFILE%" (
FOR /F "USEBACKQ DELIMS=" %%M IN ("%TMP%") DO (
	IF DEFINED FIRST (
		<NUL SET /P "=%%M"
		SET "FIRST="
	) ELSE (
		ECHO(
			<NUL SET /P "=%%M"
		)
	)
)

Variant #2

Getting the name servers of a domain under Windows.

Code for .BAT file:


@ECHO OFF
SETLOCAL EnableDelayedExpansion

:: Setting domain name to query
SET "DOMAIN=kompx.com"

:: Setting the output file
SET "OUTFILE=nameservers.txt"

:: Setting temporary files for interim storage
SET "TMP=%TEMP%\FILES_%RANDOM%.TMP"
SET "DEDUP=%TEMP%\FILES_%RANDOM%_DEDUP.TMP"
SET "SORTED=%TEMP%\FILES_%RANDOM%_SORTED.TMP"

:: Emptying temporary and output files
> "%TMP%" TYPE NUL
> "%DEDUP%" TYPE NUL
> "%SORTED%" TYPE NUL
> "%OUTFILE%" TYPE NUL

:: Getting an initial list of name servers for the domain
SET "NSLIST="
FOR /F "TOKENS=2 DELIMS==" %%G IN ('NSLOOKUP -type^=ns %DOMAIN% ^| FINDSTR /C:"nameserver"') DO (
    CALL :TRIM "%%G"
    SET "NSLIST=!NSLIST! !TRIMMED!"
)

:: Getting a list of name servers from each of the obtained name servers
FOR %%H IN (!NSLIST!) DO (
    FOR /F "TOKENS=2 DELIMS==" %%G IN ('NSLOOKUP -type^=ns %DOMAIN% %%H ^| FINDSTR /C:"nameserver"') DO (
        CALL :TRIM "%%G"
        ECHO !TRIMMED!>>"%TMP%"
    )
)

:: Removing duplicate lines
FOR /F "USEBACKQ DELIMS=" %%I IN ("%TMP%") DO (
    SET "LINE=%%I"
    IF NOT DEFINED FILES_!LINE! (
        SET "FILES_!LINE!=1"
        ECHO !LINE!>>"%DEDUP%"
    )
)

:: Sorting the contents of the temporary file alphabetically
SORT "%DEDUP%" /O "%SORTED%"

:: Removing trailing blank line from the temporary file and writing the result to the output file
SET "FIRST=1"
> "%OUTFILE%" (
    FOR /F "USEBACKQ DELIMS=" %%G IN ("%SORTED%") DO (
        IF DEFINED FIRST (
            <NUL SET /P "=%%G"
            SET "FIRST="
        ) ELSE (
            ECHO(
            <NUL SET /P "=%%G"
        )
    )
)

:: Cleaning up
DEL "%TMP%" "%DEDUP%" "%SORTED%"
EXIT /B

:: Subroutine used to remove leading and trailing spaces
:TRIM
SET "TRIMMED=%~1"

:: Removing leading spaces
FOR /F "TOKENS=* DELIMS= " %%G IN ("!TRIMMED!") DO (
	SET "TRIMMED=%%G"
)

:: Removing trailing spaces
:REMTRAIL
IF "!TRIMMED:~-1!"==" " (
	SET "TRIMMED=!TRIMMED:~0,-1!"
	GOTO REMTRAIL
)

:: Quitting :TRIM subroutine
EXIT /B

Links

  1. DNS - NSLOOKUP what is the meaning of the non-authoritative answer? serverfault.com/questions/413124/dns-nslookup-what-is-the-meaning-of-the-non-authoritative-answer#answer-413127
  2. Name server en.wikipedia.org/wiki/Name_server

Operating systems

More