Page 1 of 1

Set DNS server batch program

Posted: 11 Aug 2010 00:36
by myjet
Hi,
I know how to set the DNS server for a connection named "Wireless Network Connection" using
netsh interface ip set dns "Wireless Network Connection" static xxx.xxx.xxx.xxx
But the problem is, not every computer has its wireless connection named "Wireless Network Connection". Some are named "Wireless Network Connection 2" or "TP_Link" or other names.
So the above command don't work everywhere.
How could I tell the netsh to set the dns of a wireless connection, regardless of whatever name it is? It would be okay if we can't tell it to set a specific connection. Just to make it set every connection it found on the computer would do a great help.
Thanks in advance to anyone who could help

Re: Set DNS server batch program

Posted: 16 Aug 2010 06:29
by myjet
I found a possible solution, though I still doubt it will work on every computer. I use:
ipconfig/all>MAC.txt
to dump the configurations info and then use:
for /f "tokens=4-10 delims=: " %%b in ('"findstr "Wireless LAN adapter "" MAC.txt') do set ans=%%b %%c %%d %%e
where the input (MAC.txt) is:

Windows IP Configuration

Host Name . . . . . . . . . . . . : xx
Primary Dns Suffix . . . . . . . :
Node Type . . . . . . . . . . . . : xx
IP Routing Enabled. . . . . . . . : xx
WINS Proxy Enabled. . . . . . . . : xx

Wireless LAN adapter Wireless Network Connection 6:

Connection-specific DNS Suffix . :
Description . . . . . . . . . . . : xxx
Physical Address. . . . . . . . . :xxx
DHCP Enabled. . . . . . . . . . . : xxx
Autoconfiguration Enabled . . . . : xxx
Link-local IPv6 Address . . . . . : xxx
IPv4 Address. . . . . . . . . . . : xxx
Subnet Mask . . . . . . . . . . . : xxx
Lease Obtained. . . . . . . . . . : xxx
Lease Expires . . . . . . . . . . : xxx
Default Gateway . . . . . . . . . : xxx
DHCP Server . . . . . . . . . . . : xxx
DHCPv6 IAID . . . . . . . . . . . : xxx
DHCPv6 Client DUID. . . . . . . . : xxxx
DNS Servers . . . . . . . . . . . : xxxx
NetBIOS over Tcpip. . . . . . . . : xxx

Ethernet adapter Local Area Connection:

Media State . . . . . . . . . . . : Media disconnected
Connection-specific DNS Suffix . :
Description . . . . . . . . . . . : Realtek RTLxxx(P)/xxx(P) Family PCI-E Gigabit Ethernet NIC (NDIS 6.20) #2
Physical Address. . . . . . . . . : xxx
DHCP Enabled. . . . . . . . . . . : xx
Autoconfiguration Enabled . . . . : xxx

Tunnel adapter isatap.{xxxx}:

Media State . . . . . . . . . . . : Media disconnected
Connection-specific DNS Suffix . :
Description . . . . . . . . . . . : Microsoft ISATAP Adapter
Physical Address. . . . . . . . . : xxx
DHCP Enabled. . . . . . . . . . . : xx
Autoconfiguration Enabled . . . . : xx

Tunnel adapter isatap.{xxxx}:

Media State . . . . . . . . . . . : Media disconnected
Connection-specific DNS Suffix . :
Description . . . . . . . . . . . : xxx
Physical Address. . . . . . . . . : xxx
DHCP Enabled. . . . . . . . . . . : xx
Autoconfiguration Enabled . . . . : xx

Tunnel adapter Teredo Tunneling Pseudo-Interface:

Connection-specific DNS Suffix . :
Description . . . . . . . . . . . : Teredo Tunneling Pseudo-Interface
Physical Address. . . . . . . . . : xxx
DHCP Enabled. . . . . . . . . . . : xx
Autoconfiguration Enabled . . . . : xx
IPv6 Address. . . . . . . . . . . : xxx
Link-local IPv6 Address . . . . . : xxx
Default Gateway . . . . . . . . . : ::
NetBIOS over Tcpip. . . . . . . . : xxx

And then I might be able to use the %ans% variable to use in:
netsh interface ip set dns "%ans%" static xxx.xxx.xxx.xxx
But there is still problem because the for /f function loops for several times and set the %ans% variable to the last value of the loop and therefore set the %ans% to "Teredo Tunneling Pseudo-Interface " instead of "Wireless Network Connection 6 " (notice the space after the number 6 and the word Interface) which I intended to use. How could I make the findstr function to look for the string between "Wireless LAN adapter " and ":" and set them into %ans%? And how do I remove the space after the last word in the string (in this case, the space after the word Interface or 6)

Re: Set DNS server batch program

Posted: 16 Aug 2010 12:31
by aGerman
I can't test it, but maybe you could make it shorter

Code: Select all

for /f "tokens=4-7 delims=: " %%a in ('ipconfig/all^|findstr /c:"Wireless LAN adapter"') do set "ans=%%a %%b %%c %%d"

The problem of your code was that FINDSTR was looking for Wireless or LAN or adapter. You have to use the /c: switch that the entire string is used as literal search string.

If you're not sure about the number of tokens then you could probably use that:

Code: Select all

for /f "delims=" %%a in ('ipconfig/all^|findstr /c:"Wireless LAN adapter"') do set "ans=%%a"
set  "ans=%ans:~21,-2%"


Regards
aGerman

Re: Set DNS server batch program

Posted: 16 Aug 2010 23:05
by myjet
Great! Thanks aGerman. This works well for me. You were right that I can't be sure of the number of tokens should be used. Therefore this is my full code (tested to work regardless of what the wireless network connection name is):
for /f "delims=:" %%a in ('ipconfig/all^|findstr /c:"Wireless LAN adapter"') do set "ans=%%a"
set "ans=%ans:~21%"
netsh interface ip set dns "%ans%" static xxx.xxx.xxx.xxx