Page 1 of 1

Need help with script to retrieve wifi dns servers in win7

Posted: 25 Feb 2015 22:35
by Xboxer
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

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

Posted: 26 Feb 2015 00:20
by foxidrive
This command can give you the DNS server list.

Code: Select all

wmic NICCONFIG WHERE MACAddress!=NULL GET DefaultIPGateway,DNSServerSearchOrder

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

Posted: 26 Feb 2015 21:30
by Xboxer
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%

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

Posted: 27 Feb 2015 04:02
by foxidrive
Taking out the Skip=1 is one way to solve this.

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

Posted: 28 Feb 2015 06:46
by Xboxer
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

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

Posted: 28 Feb 2015 09:16
by Compo
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)

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

Posted: 28 Feb 2015 10:26
by foxidrive
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 . . .

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

Posted: 28 Feb 2015 11:26
by Compo
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

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

Posted: 28 Feb 2015 17:29
by Xboxer
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

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

Posted: 28 Feb 2015 18:22
by Compo
…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.).

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

Posted: 28 Feb 2015 18:58
by Xboxer
Thanks for the help, all is working well now. :wink: