-2

Im from abroad USA and i pretty like Netflix. That's why I insisted to change my DNS by one click. I wrote some code, but it doesnt work and i have no idea, why it doesn't. Here it is:

`Echo On
IF netsh interface ipv4 set dnsserver "Wi-Fi" source=dhcp==netsh interface ipv4 set dnsserver "Wi-Fi" source=dhcp DO (
netsh interface ipv4 add dnsserver "Wi-Fi" 123.123.123.123
netsh interface ipv4 add dnsserver "Wi-Fi" 123.123.123.124 index=2
ECHO DNS has changed to specific.
)
ELSE (
netsh interface ipv4 set dnsserver "Wi-Fi" source=dhcp
ECHO DNS has changed to automatic.
)
ipconfig /flushdns
`

Even can't check what's wrong, because the cmd exits automatically. Each one of the command works, so did i mess up with the "if"? Help would be appreciated <3

Gambler
  • 11
  • 3
  • [if /?](http://ss64.com/nt/if.html) – Stephan Apr 28 '16 at 13:07
  • basic troubleshooting: a) run with `echo on` (you did that - good) b) start it from `cmd`prompt ("Dos-Box") instead of doubleclicking it, so the window stays open on errors to let you read them. – Stephan Apr 28 '16 at 13:23

2 Answers2

1
IF "netsh interface ipv4 set dnsserver "Wi-Fi" source=dhcp" == "netsh interface ipv4 set dnsserver "Wi-Fi" source=dhcp" (

a) enclose your strings in qoutes if there may be spaces.

b) there is no DO with if

c) ) and ELSE and ( have to be on the same physical line:

) else (
Stephan
  • 53,940
  • 10
  • 58
  • 91
  • Now it works, but it doesnt in a way that i expected. Is there any expression that makes me the result of command exqual to a written data? Sort of: `C:/Users/Administrator>netsh interface ipv4 show "Wi-Fi"` `Configuration of interface Wi-Fi:` `DNS servers configured trough DHCP: 192.168.2.1` `Register with suffix: Primary only` I mean, I wanna use: `if "netsh interface ipv4 show "Wi-Fi" ==` result of this command rewrited above `(...)`, then it would work in the right way – Gambler Apr 29 '16 at 00:12
1

I enjoyed my problem, thanks to you i've got a solution. I used file comparision command to do this in the way I imagined. Here it is:

@echo off
REM At first, define an interface you're using by command "netsh interface ipv4 show dnsserver".
REM Depends on language the names could be diffrent.
REM Example: Wireless Network Connection / Local Area Connection / Wi-Fi

set INTERFACE=Wi-Fi
set DNS1=123.123.123.123
set DNS2=123.123.123.124

REM Here we goes:
netsh interface ipv4 show dnsserver "%INTERFACE%" > C:\DNS.txt
fc /b DNS.txt DNX.txt > nul
IF errorlevel 1 (
netsh interface ipv4 set dnsserver "%INTERFACE%" source=dhcp
ECHO DNS has changed to automatic. 
ECHO.
) else (
netsh interface ipv4 add dnsserver "%INTERFACE%" %DNS1%
netsh interface ipv4 add dnsserver "%INTERFACE%" %DNS2% index=2
ECHO DNS has changed to static. 
ECHO.
)
ipconfig /flushdns > nul
netsh interface ipv4 show dnsserver "%INTERFACE%" > DNX.txt
pause

It seems to working :) Again thanks for help :)

Gambler
  • 11
  • 3