我正在使用(尝试)批处理文件,以便在断开与交换机的连接时启动重新启动。我需要的是让PC ping交换机的IP地址,当连接丢失时,PC重新启动。我找到了一些来源,并使用多个代码来实现这一目标。下面的脚本有效但我也希望确定一些额外的功能。
我更希望让批处理文件执行上述所有任务,但也发现我只能完成#2& #3通过在Windows中添加计划任务。
以下是我正在使用的当前脚本。任何信息都表示赞赏。
@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/)
答案 0 :(得分:2)
此脚本针对 1 和 2 点。它的方式如下:
- 调用connection_test
函数以获取网络状态
- 根据状态的结果,它会继续尝试(如果需要)
- 如果不需要再次尝试,它会等待300
变量中设置的timeout_secs
秒(5分钟)并再次启动该过程(直到失败)
- failure_count
是在认为down
网络状态之前ping失败的最大次数
- max_connection_error_count
是重新启动前的最大计数或重试次数
对于 3 点,将此脚本添加到启动中(或使用任务计划程序启动时启动)"%USERPROFILE%\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup"
对于 4 点,我不知道如何批量执行此操作,也许可以尝试: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
答案 1 :(得分:1)
PS。 要使此脚本在后台运行,只需将其安装为服务即可。
下载nssm https://nssm.cc/,然后
像这样跑,
< \目录-其中-解压\> nssm.exe安装< \ service-name-of-choice-here>
会弹出一个家伙窗口并填写空白
你已经完成了。查看将列出的Windows服务自动启动。
享受