Skip to content

Instantly share code, notes, and snippets.

@CallumWatkins
Last active March 20, 2024 08:58
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save CallumWatkins/54c19fee67aa2ed01e60dd185663b1be to your computer and use it in GitHub Desktop.
Save CallumWatkins/54c19fee67aa2ed01e60dd185663b1be to your computer and use it in GitHub Desktop.
A batch file to periodically check your internet connection, restart on failed connection and write to log. License: MIT
@echo off
mode con: cols=55 lines=12
title Network Checker
REM Server to be pinged
SET server=google.co.uk
REM Size of packet to be send to server (bytes)
SET packetSize=1
REM Network info
SET netSSID=[SSID]
SET netName=[Network Name]
SET netInterface=[Network Interface]
REM Pause time between each network check (seconds)
SET successfulTimeout=10
REM Pause time after reconnection before next check (seconds)
SET failedTimeout=5
REM Write failed network connections to log (Boolean)
SET writeToLog=1
REM Do not change
SET lastFail=never
SET successfulRepetitions=0
REM Check internet connection
:check
cls
ECHO Successful repetitions: %successfulRepetitions%
ECHO Last fail: %lastFail%
ECHO.
ECHO Pinging %server%...
PING -n 1 -l %packetSize% %server% >NUL
IF %errorlevel% EQU 0 GOTO successful
GOTO failed
REM Internet connection succeeded
:successful
color 0A
SET /a successfulRepetitions+=1
ECHO Ping successful!
TIMEOUT %successfulTimeout%
GOTO check
REM Internet connection failed
:failed
color 0C
SET lastFail=%time:~-11,2%:%time:~-8,2%:%time:~-5,2% - %date%
ECHO Ping failed!
ECHO Disconnecting network interface...
netsh wlan disconnect interface="%netInterface%"
TIMEOUT 4 >NUL
ECHO Reconnecting network interface...
netsh wlan connect ssid="%netSSID%" Name="%netName%" Interface="%netInterface%"
IF %writeToLog%==1 (
ECHO Ping to %server% failed at %lastFail% >> NetworkLog.txt
ECHO Previous successful repetitions: %successfulRepetitions% >> NetworkLog.txt
ECHO ==================== >> NetworkLog.txt
)
SET successfulRepetitions=0
TIMEOUT %failedTimeout%
GOTO check
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment