批处理文件以在网络连接丢失时重新启动PC [英] Batch File to reboot pc on network connectivity loss

查看:72
本文介绍了批处理文件以在网络连接丢失时重新启动PC的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当与交换机的连接断开时,我正在使用(尝试)批处理文件来启动重新引导.我需要的是让PC ping交换机的IP地址,并且当连接丢失时,PC会重新启动.我找到了一些资源,并且正在使用多个代码片段来实现此目的.下面的脚本有效,但是我还想确定几个附加功能.

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. 仅在3次尝试失败后PC才会重新启动
  2. PC每5分钟ping IP检查一次活动连接
  3. 批处理在启动时运行
  4. 在重新建立连接后发送电子邮件,通知我连接断开.

我希望让批处理文件执行上述所有任务,但同时发现我只能完成#2&任务.#3通过在Windows中添加计划任务.

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


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

推荐答案

  1. 仅在3次失败尝试后PC才会重新启动
  2. PC每5分钟ping IP检查一次活动连接
  3. 批处理在启动时运行
  4. 在重新建立连接后发送电子邮件,通知我连接断开."

此脚本处理点 1 2 .这样做的方式如下:
-调用 connection_test 函数以获取网络状态
-根据状态结果,它会继续重试(如果需要)
-如果不需要再次尝试,它将等待 timeout_secs 变量中设置的 300 秒(5分钟),然后再次启动该过程(直到失败)
- failure_count 是在认为 down 网络状态之前ping故障的最大次数.
- max_connection_error_count 是要重新启动之前的最大次数或重试次数

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

对于点 3 ,将此脚本添加到启动中(或使用任务计划程序在启动时启动)%USERPROFILE%\ AppData \ Roaming \ Microsoft \ Windows \ Start Menu \ Programs\ Startup"

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"

对于点 4 ,我不知道如何批量执行此操作,请尝试以下操作:

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 

这篇关于批处理文件以在网络连接丢失时重新启动PC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆