Set DNS server batch program

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
myjet
Posts: 10
Joined: 10 Aug 2010 18:50

Set DNS server batch program

#1 Post by myjet » 11 Aug 2010 00:36

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

myjet
Posts: 10
Joined: 10 Aug 2010 18:50

Re: Set DNS server batch program

#2 Post by myjet » 16 Aug 2010 06:29

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)
Last edited by myjet on 16 Aug 2010 23:08, edited 2 times in total.

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Set DNS server batch program

#3 Post by aGerman » 16 Aug 2010 12:31

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

myjet
Posts: 10
Joined: 10 Aug 2010 18:50

Re: Set DNS server batch program

#4 Post by myjet » 16 Aug 2010 23:05

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

Post Reply