# Batch File to reboot pc on network connectivity loss



## jordanvalliere (Jun 12, 2014)

I am using (trying) a batch file to initiate a reboot when connectivity to a switch is lost. what I need is to have the PC ping the IP address of the switch and when the connection has been lost, the PC reboots. I have found a few sources and am using pieces of multiple codes to achieve this. The script below works however I also would like to have a couple additional features determined.


PC reboots only after 3 failed attempts
PC pings the IP checking for active connection every 5 minutes
Batch runs on startup
Email is sent after connection is re-established to notify me that there was a loss in connection.
 I would prefer to have the batch file performing all the above tasks but have also found that I may only accomplish #2 & #3 by adding a scheduled task in Windows.
Below is the current script I am using. Any information is appreciated.

The issue with the current script is that when there is an active connection the script continues to run. I need this application to every 5 minutes ping an IP or DNS 3 times and if the network state is up the application closes.


```
@echo
set ping_ip=127.0.0.1
set failure_count=0
set timeout_secs=15
set connection_error_count=0
set max_connection_error_count=3


:start
:: calling the ping function
call :connection_test

:: Processing the network "up" state
if "%network_state%"=="up" (
    echo INFO: You have an active connection.
    set connection_error_count=0
) else (
    set /a connection_error_count+=1
)

:: Processing the network "down" state
if "%network_state%"=="down" (
    if %connection_error_count% geq %max_connection_error_count% (
        echo ERROR: You do not have an active connection.
        goto poweroff
    ) else (
        echo INFO: FAILURE: That failed [%connection_error_count%] times, NOT good. lets try again... 
        goto start
    )
)

timeout /t %timeout_secs%
goto start



:: connection_test function
goto skip_connection_test
:connection_test
:: Getting the successful ping count
echo INFO: Checking connection, please hang tight for a second...
for /f "tokens=5 delims==, " %%p in ('ping -n 4 %ping_ip% ^| findstr /i "Received"') do set ping_count=%%p

:: Check the ping_count against the failure_count
if "%ping_count%" leq "%failure_count%" (
    set network_state=down
) else (
    set network_state=up
)
goto :eof
:skip_connection_test




:: Power off 
:poweroff
echo INFO: Restarting PC in 60 seconds.  Press any key to abort.
shutdown -r -t 60 -f
pause > nul
shutdown -a
goto end

:end
```
-------
Tech Support Guy System Info Utility version 1.0.0.2
OS Version: Microsoft Windows 7 Professional, Service Pack 1, 64 bit
Processor: Intel(R) Core(TM) i5-3230M CPU @ 2.60GHz, Intel64 Family 6 Model 58 Stepping 9
Processor Count: 4
RAM: 3661 Mb
Graphics Card: Intel(R) HD Graphics 4000, 1638 Mb
Hard Drives: C: Total - 261589 MB, Free - 194552 MB; Q: Total - 16029 MB, Free - 2293 MB;
Motherboard: LENOVO, 68852BU
Antivirus: Symantec Endpoint Protection, Not Updated


----------



## foxidrive (Oct 20, 2012)

Do you mean that a failed ping, every 5 minutes for 15 minutes is the trigger?


----------



## jordanvalliere (Jun 12, 2014)

Right now I have the timeout set to 15 seconds for testing. I change it to 300 seconds when normal. This part works. I run the application and a ping test will run every 5 minutes. What happens is, the ping test never stops. I am trying to have the script on start run a ping test every 5 minutes up to 3 times. If the ping test passes after 3 attempts, the application should close. If the ping attempt fails, the application should force a PC reboot. 

-Thanks


----------

