-
-
Save rcgraves/a88cbac1c60a6badce1642c860ac35dc to your computer and use it in GitHub Desktop.
set static dns for network connection using windows console cmd script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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