Batch script of nslookup of bulk IP's corresponding to the DNS servers

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
ank_stal
Posts: 1
Joined: 04 Nov 2020 23:54

Batch script of nslookup of bulk IP's corresponding to the DNS servers

#1 Post by ank_stal » 04 Nov 2020 23:59

I have been working on the script but it checks the IP's corresponding to the primary dns only.

Now I'm trying to make changes in this one by checking the IP to specified dns servers. But I also don't want to flood the output with multiple entries of IP corresponding to all dns servers.

Code: Select all

@echo OFF
setlocal enabledelayedexpansion enableextensions
del results.txt 2>nul
set me=%~n0
set parent=%~dp0
set "outputfile=results.txt"
set "inputfile=ip.txt"
set "dnsfile=dns.txt"
FOR /F %%i in (%dnsfile%) do (
    FOR /F %%j in (%inputfile%) do (
        FOR /F "delims=: tokens=2" %%k in ('nslookup %%j %%i ^| find /i "Name:"') do @echo %%j,%%k >> %outputfile%
    )
)
This gives the output corresponding to all the dns servers but I want only one entry in the output for all the IP's. If the script gets the hostname of the IP corresponding to one dns server it should print it in the output and go to the next IP instead of checking the same ip corresponding to the different dns server. But I'm stuck at this point not able to skip the inside loop and get to the next IP.

Eg:

ip.txt:

10.x.x.5
10.x.x.10
dns.txt:

172.x.x.6
172.x.x.2
Output I get with the script:

Results.txt

10.x.x.5,server1
10.x.x.10,server2
10.x.x.5,server1
10.x.x.10,server2
Output I want:

Results.txt

10.x.x.5,server1
10.x.x.10,server2
Last edited by aGerman on 05 Nov 2020 04:55, edited 1 time in total.
Reason: Please use code formatting.

penpen
Expert
Posts: 1872
Joined: 23 Jun 2013 06:15
Location: Germany

Re: Batch script of nslookup of bulk IP's corresponding to the DNS servers

#2 Post by penpen » 07 Nov 2020 04:06

The following idea might help you (untested):

Code: Select all

FOR /F %%j in (%inputfile%) do "set map[%%~j]="
FOR /F %%i in (%dnsfile%) do (
    FOR /F %%j in (%inputfile%) do (
        FOR /F "delims=: tokens=2" %%k in ('nslookup %%j %%i ^| find /i "Name:"') do (
		if not not defined map[%%~j] (
			"set map[%%~j]=%%~k"
			>> %outputfile%	echo %%j,%%k
		)
	)
    )
)
penpen

Post Reply