Page 1 of 1

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

Posted: 04 Nov 2020 23:59
by ank_stal
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

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

Posted: 07 Nov 2020 04:06
by penpen
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