1

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.

  1. PC reboots only after 3 failed attempts
  2. PC pings the IP checking for active connection every 5 minutes
  3. Batch runs on startup
  4. 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.

@echo
ECHO Checking connection, please hang tight for a second...
PING -n 4 216|find "Reply from " >NUL
IF NOT ERRORLEVEL 1 goto :SUCCESS
IF     ERRORLEVEL 1 goto :TRYAGAIN

:TRYAGAIN
ECHO FAILURE!
ECHO That  failed NOT good. lets try again... 
@echo
PING -n 4 216|find "Reply from " >NUL
IF NOT ERRORLEVEL 1 goto :SUCCESS2
IF     ERRORLEVEL 1 goto :FAILURE


:SUCCESS
ECHO You have an active connection.
pause
goto :END

:SUCCESS2
ECHO network connectivity exists but there may be an issue still
goto :END

:FAILURE
ECHO You do not have an active connection.
pause
ECHO Restarting PC in 60 seconds.  Run SHUTDOWN -a to abort. 
pause
SHUTDOWN -r -t 60 -f

:END

Sources: (http://www.cam-it.org/index.php?topic=2786.0) (http://www.instructables.com/id/Shutdown-restart-or-hibernate-your-computer-on-a/)

Alex
  • 917
  • 5
  • 11
user3570433
  • 21
  • 1
  • 5

2 Answers2

3
  1. PC reboots only after 3 failed attempts
  2. PC pings the IP checking for active connection every 5 minutes
  3. Batch runs on startup
  4. Email is sent after connection is re-established to notify me that there was a loss in connection."

This script addresses points 1 and 2. The way it does so is the following:
- calls the connection_test function to get the status of the network
- Based on the result from the status, it continues to try again (if needed)
- If not needed to try again, it waits 300 secs (5 mins) as set in the timeout_secs variable and starts the process again (until fails)
- failure_count is the maximum count of ping failures before deeming down network state.
- max_connection_error_count is the maximum count or retries before going to reboot

For point 3, add this script into the startup (or use task scheduler to start at startup) "%USERPROFILE%\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup"

For point 4, I dont know how to do this in batch, maybe try this: http://www.robvanderwoude.com/email.php

@echo off
set ping_ip=1.1.1.1
set failure_count=0
set timeout_secs=300
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 
Alex
  • 917
  • 5
  • 11
  • First, thank you much for the script. I have tried 4 seperate tests and I am still having an issue. 1st test I have the script pinging 1.1.1.1 and with an active connection the script works. When I ping google (74.125.228.36) it works but the application needs to be manually terminated. oddly, when I don't have an active connection and I ping google, there isnt a network failure. I have the logs if needed. Any suggestions? Thank you – user3570433 May 29 '14 at 12:34
  • I updated the script, see if this helps you. Also, please share the logs of what's not working – Alex May 29 '14 at 15:59
  • the script works perfect when loss of network connection occurs. When the script is running and there is an active connection present how can i have the script end after 3 successful attempts. Currently the script just continues to run. Additionally, I would like to make this script run as transparent to the user as possible. Thanks in advance - Jordan – user3570433 Jun 03 '14 at 16:34
  • Great script! I used "shutdown **-g** -t 60 -f", and used task scheduler to fire it on an error event from my sometimes cranky Marvell Yukon ethernet adapter. (Which can for some reason only be restarted after it fails by rebooting the computer.) – Matthew W Feb 09 '18 at 21:19
  • Unfortunately sometimes ping check can fail because Windows ping command marks received ping packets that have Destination host unreachable , for example: ping -n 4 10.0.0.2 Pinging 10.0.0.2 with 32 bytes of data: Reply from 10.0.0.1: Destination host unreachable. Reply from 10.0.0.1: Destination host unreachable. Reply from 10.0.0.1: Destination host unreachable. Reply from 10.0.0.1: Destination host unreachable. Ping statistics for 10.0.0.2: Packets: Sent = 4, Received = 4, Lost = 0 (0% loss), – nkef Aug 03 '23 at 07:51
1

PS. To make this script run in the background, just install it as a service.

  1. download the nssm https://nssm.cc/ ,then

  2. run like so,

<\directory-where-unzipped\> nssm.exe install <\service-name-of-choice-here>

  1. a guy window will pop up and you fill in the blanks

  2. you're done. view the windows services where it will be listed to start automatically.

Enjoy