Skip to content

Instantly share code, notes, and snippets.

@rcgraves
Forked from n074v41l4bl34u/set-dns.cmd
Last active June 19, 2016 12:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rcgraves/a88cbac1c60a6badce1642c860ac35dc to your computer and use it in GitHub Desktop.
Save rcgraves/a88cbac1c60a6badce1642c860ac35dc to your computer and use it in GitHub Desktop.
set static dns for network connection using windows console cmd script
@echo off
set /a whichdns=(%RANDOM%*2/32768)+1
if %whichdns% equ 1 (
set DNS1=137.22.198.40 && set DNS2=137.22.198.41
) else (
set DNS1=137.22.198.41 && set DNS2=137.22.198.40
)
route -4 print | findstr "137.22.195.254" && set DNS1=137.22.194.250
route -4 print | findstr "137.22.94.254" && set DNS1=137.22.94.250
REM i = Enabled, j = Connected, k = Type, l = Interface Name
for /f "skip=2 tokens=1,2,3*" %%i in ('netsh int show interface') do (
if %%i equ Enabled if %%j equ Connected (
LOCALDNS=0
netsh int ipv4 show dns "%%l" | findstr "127.0.0.1" && set LOCALDNS=1
netsh int ipv4 show dns "%%l" | findstr "137.22.1.13 137.22.1.14 137.22.128.2"
if %ERRORLEVEL% equ 0 (
netsh int ipv4 set dns name="%%l" static %DNS1% primary validate=no
netsh int ipv4 add dns name="%%l" %DNS2% index=2 validate=no
if %LOCALDNS% equ 1 (
netsh int ipv4 add dns name="%%l" 127.0.0.1 index=3 validate=no
) else (
netsh int ipv4 add dns name="%%l" 8.8.8.8 index=3 validate=no
)
)
)
)
goto exit
:exit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment