Windows Network Watchdog

I needed a Network Watchdog for a standalone WiFi connected windows pc running a dashboard and came up with the below.

Create a textfile and call it network_watchdog.cmd. Place it in c:\ (or wherever you place your scripts).
Use Windows “Task Scheduler”. Set to run at “Startup” with a delay and interval of your choice. Remember to tick “Run with highest privileges”.

@echo off

TITLE Network Connection Watchdog

:: Last command will only be executed if all pings fail.

set /a pc=0
set /a total=0

:: Your preferences
set if="Wi-fi" &:: Find your interface name with [netsh interface show interface].
set ip=192.168.0.1
set /a pt=10 &:: Set amount of pings to be executed.

:loop
set /a pc+=1
ping %ip% -n 1 -w 100 | find "TTL=" >NUL 2>&1
for /f %%a in ('echo %errorlevel%') do set p%pc%=%%a

set /a total+=p%pc%
if not %pc%==%pt% goto loop

if not %total%==%pt% (
goto eof
) else (
netsh interface set interface %if% disable
ping localhost -n 5 >NUL 2>&1
netsh interface set interface %if% enable
)

:eof
exit

If you need it silent create a .vbs file containing the below and run that from “Task scheduler” instead.

command = "powershell.exe -nologo -command C:\network_watchdog.cmd"
 set shell = CreateObject("WScript.Shell")
 shell.Run command,0

To be able to do the above you’ll have to run the following from an elevated powershell.

set-executionpolicy remotesigned

Leave a Reply

Your email address will not be published. Required fields are marked *