How can I automatically reboot a windows machine that lost connectivity?

Yep, it’s a windows question again folks!

A while back, I wrote a cute little batch script for a Win2k3 server that is a VMWare guest, that kept losing connectivity to the VMNetwork. The guest itself was up and fine, however it just could not communicate with anything until it was rebooted.

So, I wrote the script to detect when it could no longer reach its own gateway (or the VM host for that matter), and would reboot itself as gracefully as possible. I configured a “task” to launch the script at boot, and removed the loop protection and stuff from the task properties (since it *is* a loop script, after all).

I got asked about this script enough that it warranted its own post on my blog. And, I’m just going to paste the script right here into the post itself (at bottom) rather than as an attachment. I hope that works for you…

Enjoy!
Here’s the script:

@ECHO OFF

rem Run this script once, at start-up, and it
rem will loop itself forever. Hopefully.

rem The dash-n option in the ping controls how
rem long each loop is, as it does one ping per
rem second. Of course you should set the host
rem that you are pinging to something like the
rem default gateway of the server.

rem If *ANY* of the pings get replies, then
rem the return code is zero, and the destination
rem host is considered to be up.

rem If *NO* ping replies come back for and entire
rem loop, then the box will reboot. This means
rem that communication could be lost for *almost*
rem two complete loops in order to reboot. To be
rem specific, it could be as quick as:
rem - one loop plus one ping
rem ...or as long as:
rem - two loops minus one ping

:LOOP
ping -n 10 192.168.1.1

IF NOT ERRORLEVEL 1 GOTO LOOP
IF ERRORLEVEL 1 GOTO REBOOT

:REBOOT
echo We will reboot in 5 seconds!
rem For windows xp, the following command should
rem use dashes (-) for all the flags. For windows
rem server 2003, use forward slashes for the flags.
shutdown /r /f /t 5 /c "Pings failed. Rebooting." /d 5:20

Leave a Comment

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