Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @echo off
- setlocal
- rem Context: https://www.reddit.com/r/windows/comments/16anonp/apply_network_settings_and_if_failed_revert_back/
- rem Revision 2.
- rem
- rem https://www.reddit.com/user/jcunews1
- rem https://pastebin.com/u/jcunews
- rem https://greasyfork.org/en/users/85671-jcunews
- rem === CONFIG BEGIN ===
- set "DeviceName=Local Area Connection"
- set RemoteIpToCheck=8.8.8.8
- set RemoteIpToCheckTimeoutMilliseconds=3000
- set ErrorMaxDurationSeconds=30
- rem === CONFIG END ===
- >nul netsh interface ip show addresses "%DeviceName%"
- if errorlevel 1 (
- echo Network device name "%DeviceName%" is not found.
- pause
- goto :eof
- )
- set ErrorStartTimestamp=0
- :check
- set DhcpEnabled=0
- for /f "tokens=3 delims= " %%A in ('netsh interface ip show addresses "%DeviceName%" ^| find "DHCP enabled"') do if "%%A" == "Yes" set DhcpEnabled=1
- if %DhcpEnabled% == 1 (
- set ErrorStartTimestamp=0
- echo %time%: DHCP is enabled.
- >nul timeout 1
- goto check
- )
- echo %time%: DHCP is disabled. Checking internet access...
- >nul ping -n 1 -w %RemoteIpToCheckTimeoutMilliseconds% %RemoteIpToCheck%
- if not errorlevel 1 (
- set ErrorStartTimestamp=0
- echo %time%: Has internet access.
- >nul timeout 1
- goto check
- )
- for /f "tokens=1,2,3,4 delims=:." %%A in ("%time%") do set /a t=%%A*360000+%%B*6000+%%C*100+%%D
- if %ErrorStartTimestamp% == 0 set ErrorStartTimestamp=%t%
- if %t% lss %ErrorStartTimestamp% set /a ErrorStartTimestamp-=8640000
- set /a elapsed=(t-ErrorStartTimestamp)/100
- echo %time%: No internet access for %elapsed% seconds.
- if %elapsed% lss %ErrorMaxDurationSeconds% goto check
- echo %time%: Enabling DHCP...
- netsh interface ip set address "%DeviceName%" dhcp
- if not errorlevel 1 echo %time%: DHCP has been enabled.
- set ErrorStartTimestamp=0
- >nul timeout 1
- goto check
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement