1) You have predictable settings to each ISP
2) Don't need to use 2 labels to each ISP settings
3) Replace find /i ... errorlevel
to findstr && || (operators)
4) Replace your labels to if...() else ()...
@echo off && setlocal enabledelayedexpansion
cls & echo/ & for %%i in (1,2
)do "%__APPDIR__%ping.exe" -n 1 dns.ser.ver.%%~i|%__APPDIR__%findstr.exe "TTL=" >nul && (
echo/ DNS ISP-%%~i ping/link status: UP^^!! && set "_isp_srv=%%~i" && goto :_change-isp_:
)|| echo/ DNS ISP-%%i ping/link status: DOWN^^!! & if "%%~i" == "2" endlocal && goto :EOF
:_change-isp_:
set "_cmd_netsh=%__APPDIR__%netsh.exe interface ipv4" && if not "!_isp_srv!" == "1" (
!_cmd_netsh! set address name="Ethernet 2" source=dhcp && !_cmd_netsh! set dnsservers name="Ethernet 2" source=dhcp
) else (!_cmd_netsh! set address name="Ethernet 2" static 108.129.156.199 255.255.255.0 108.129.156.1 store=persistent
!_cmd_netsh! set dnsservers name="Ethernet 2" source=static address="108.129.159.31" validate=no
!_cmd_netsh! add dnsservers name="Ethernet 2" address="108.129.159.33" validate=no index=2 )
rem./ do more tasks here, after that add "endlocal" at the end: ===^> && endlocal && goto :EOF
For this to work for you with real name/IP, some adjustments to some lines are needed to deal with real names/ip for DNS.SER.VER.1 and DNS.SER.VER.2.
cls & echo/ & for %%i in ( 1,2 //> replace 1,2 to real name or IP
)do "%__APPDIR__%ping.exe" -n 1 dns.ser.ver.%%~i //> replace for using only the variable in loop dns.ser.ver. %%i
set "_cmd_netsh=%__APPDIR__%netsh.exe interface ipv4" && if not "!_isp_srv!" == "1" ( //> replace "1" to the real name for DNS.SER.VER.1
)|| echo/ DNS ISP-%%i //> replace to the var in loop %%i
& if "%%~i" == "2" //> replace to the real name for DNS.SER.VER.2
- The same code in "conventional formatting"
@echo off && setlocal enabledelayedexpansion
cls
echo/
for %%i in (1,2) do (
"%__APPDIR__%ping.exe" -n 1 dns.ser.ver.%%~i|%__APPDIR__%findstr.exe "TTL=" >nul && (
echo/ DNS ISP-%%~i ping/link status: UP^^!!
set "_isp_srv=%%~i"
goto :_change-isp_:
) || (
echo/ DNS ISP-%%i ping/link status: DOWN^^!!
if "%%~i" == "2" endlocal && goto :EOF
)
)
:_change-isp_:
set "_cmd_netsh=%__APPDIR__%netsh.exe interface ipv4"
if not "!_isp_srv!" == "1" (
!_cmd_netsh! set address name="Ethernet 2" source=dhcp
!_cmd_netsh! set dnsservers name="Ethernet 2" source=dhcp
) else (
!_cmd_netsh! set address name="Ethernet 2" static 108.129.156.199
255.255.255.0 108.129.156.1 store=persistent
!_cmd_netsh! set dnsservers name="Ethernet 2" source=static
address="108.129.159.31" validate=no
!_cmd_netsh! add dnsservers name="Ethernet 2" address="108.129.159.33" validate=no index=2
)
rem./ do more tasks here, after that add "endlocal" at the end: ===^> && endlocal && goto :EOF
This question suggests that you want to change the actual cable settings from current to another ISP ...
So, you can also do this with simpler code, like:
yes, maybe it's not that simple.
@echo off && setlocal enabledelayedexpansion && cls & echo/
pushd %__APPDIR__% & set "_cmd_netsh=netsh.exe interface ipv4" && ping.exe -n 1 108.129.156.1|findstr "TTL=">nul && (
!_cmd_netsh! set address name="Ethernet 2" source=dhcp && !_cmd_netsh! set dnsservers name="Ethernet 2" source=dhcp
)||( !_cmd_netsh! set address name="Ethernet 2" static 108.129.156.199 255.255.255.0 108.129.156.1 store=persistent
!_cmd_netsh! set dnsservers name="Ethernet 2" source=static address="108.129.159.31" validate=no
!_cmd_netsh! add dnsservers name="Ethernet 2" address="108.129.159.33" validate=no index=2 )
podp & endlocal && goto :EOF
Sorry my limited English
Maybe this Post will help you :) https://stackoverflow.com/questions/22439971/determine-if-ethernet-is-plugged-unplugged-batch-code
– Shayvin – 2020-02-03T15:06:24.133Thanks, but in my case both connections (interfaces) has the same name "Ethernet 2", because it is essentially the same interface, I just want to change its settings. – ZygD – 2020-02-03T16:44:23.733