Need help with script to retrieve wifi dns servers in win7

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Xboxer
Posts: 25
Joined: 27 Nov 2014 13:58

Need help with script to retrieve wifi dns servers in win7

#1 Post by Xboxer » 25 Feb 2015 22:35

I need a script that can capture both the (primary + secondary) dns servers into a variable
the batch script below captures Only the Primary and Name of the google dns but now I also need to add the secondary dns (8.8.4.4)
Thanks for the help!

Code: Select all

@Echo Off 
:: Get IPv4 DNS IP shows primary as 8.8.8.8
For /F "Tokens=2" %%A In ('Echo quit^|Nslookup^|Find "Address:"') Do Set Primary_IP=%%A
:: Get DNS Name
For /F "Tokens=2" %%C In ('Nslookup %Primary_IP%^|Find "Name:"') Do Set DnsName=%%C
:: Output DNS IP and Server Name
Echo. %Primary_IP%  %DnsName%
Pause

foxidrive
Expert
Posts: 6033
Joined: 10 Feb 2012 02:20

Re: Need help with script to retrieve wifi dns servers in wi

#2 Post by foxidrive » 26 Feb 2015 00:20

This command can give you the DNS server list.

Code: Select all

wmic NICCONFIG WHERE MACAddress!=NULL GET DefaultIPGateway,DNSServerSearchOrder

Xboxer
Posts: 25
Joined: 27 Nov 2014 13:58

Re: Need help with script to retrieve wifi dns servers in wi

#3 Post by Xboxer » 26 Feb 2015 21:30

foxidrive wrote:This command can give you the DNS server list.

Code: Select all

wmic NICCONFIG WHERE MACAddress!=NULL GET DefaultIPGateway,DNSServerSearchOrder


foxidrive thanks for the wmic command, I used the below script but it reads out the top line and I only need the second line down. Do you have a solution for this? thanks again for your help.

Code: Select all

For /F "Tokens=* Skip=1" %%a in ('wmic nicconfig get dnsserversearchorder ^|find ","') do set dnsip=%%a
Echo. %dnsip%

foxidrive
Expert
Posts: 6033
Joined: 10 Feb 2012 02:20

Re: Need help with script to retrieve wifi dns servers in wi

#4 Post by foxidrive » 27 Feb 2015 04:02

Taking out the Skip=1 is one way to solve this.

Xboxer
Posts: 25
Joined: 27 Nov 2014 13:58

Re: Need help with script to retrieve wifi dns servers in wi

#5 Post by Xboxer » 28 Feb 2015 06:46

foxidrive wrote:Taking out the Skip=1 is one way to solve this.

Thanks for your suggestion but I have already tried that and get the same results, found this wmic command that outputs the server name with dns ip at http://blog.commandlinekungfu.com/2010/ ... erver.html and think this may work if it can be written correctly in a batch For loop.
Thanks for the help, xboxer

Code: Select all

C:\> for /f "tokens=1-3 delims={}, " %a in
  ('wmic nicconfig get dnsserversearchor ^| find ","')
  do @nslookup %a & @nslookup %b 2>gt;nul & @nslookup %c 2>gt;nul

Compo
Posts: 507
Joined: 21 Mar 2014 08:50

Re: Need help with script to retrieve wifi dns servers in wi

#6 Post by Compo » 28 Feb 2015 09:16

Xboxer wrote:

Code: Select all

C:\> for /f "tokens=1-3 delims={}, " %a in
  ('wmic nicconfig get dnsserversearchor ^| find ","')
  do @nslookup %a & @nslookup %b 2>gt;nul & @nslookup %c 2>gt;nul

Can you not replace that awful commandline with this?

Code: Select all

C:\>For /F "Skip=1 Delims={}" %A In ('WMIc NICConfig Get DNSServerSearchOrder') Do @(For %B In (%~A) Do @NSLookUp %B)

foxidrive
Expert
Posts: 6033
Joined: 10 Feb 2012 02:20

Re: Need help with script to retrieve wifi dns servers in wi

#7 Post by foxidrive » 28 Feb 2015 10:26

Xboxer wrote:
foxidrive wrote:Taking out the Skip=1 is one way to solve this.

Thanks for your suggestion but I have already tried that and get the same results


It works fine here:

Code: Select all

d:\>type a.bat
@echo off
For /F "Tokens=*" %%a in ('wmic nicconfig get dnsserversearchorder ^|find ","') do set dnsip=%%a
Echo. %dnsip%
pause

d:\>a
 {"198.242.0.51", "221.39.132.12", "198.252.235.14"}
Press any key to continue . . .

Compo
Posts: 507
Joined: 21 Mar 2014 08:50

Re: Need help with script to retrieve wifi dns servers in wi

#8 Post by Compo » 28 Feb 2015 11:26

Based on the output from foxidrive, (above), you may wish to change my commandline to:

Code: Select all

C:\>For /F "Skip=1 Delims={}" %A In ('WMIc NICConfig Get DNSServerSearchOrder') Do @(For %B In (%A) Do @NSLookUp %~B)
I hadn't noticed that the output in the linked webpage had each address in the array in double quotes.

The following script should output any non failed lookup Addresses with their Names:

Code: Select all

@Echo Off
For /F "Skip=1 Delims={}" %%A In ('WMIc NICConfig Get DNSServerSearchOrder'
   ) Do (For %%B In (%%A) Do (For /F "Tokens=1-2" %%C In (
         'NSLookUp %%~B 2^>Nul') Do If %%C Equ Name: Echo;%%~B=%%D))
Timeout -1
Last edited by Compo on 28 Feb 2015 18:16, edited 1 time in total.

Xboxer
Posts: 25
Joined: 27 Nov 2014 13:58

Re: Need help with script to retrieve wifi dns servers in wi

#9 Post by Xboxer » 28 Feb 2015 17:29

Foxi, I stand corrected I did a typo and your suggestion of removing the skip will now work.
so thank your for that.

Compo, I like how you think, I was also thinking of adding the server name along with
the IP and yours will do that. If possible tho can you have the info set to a variable
as I will be using it in a audit report with other scripts.
Thanks, xboxer

Compo
Posts: 507
Joined: 21 Mar 2014 08:50

Re: Need help with script to retrieve wifi dns servers in wi

#10 Post by Compo » 28 Feb 2015 18:22

…well it all depends upon how you're wanting to use the information, you state a variable but there may be several variables, (%PrimaryIP%, %PrimaryName%, %SecondaryIP%, %SecondaryName%, %TertiaryIP%, %TertiaryName%, %QuarternaryIP%, %QuarternaryName% etc.).

Xboxer
Posts: 25
Joined: 27 Nov 2014 13:58

Re: Need help with script to retrieve wifi dns servers in wi

#11 Post by Xboxer » 28 Feb 2015 18:58

Thanks for the help, all is working well now. :wink:

Post Reply