电源管理批处理文件,用于在另一台电脑关闭/休眠/失去连接时关闭电脑



这是我在这里的第一篇文章,所以请原谅我做错了什么。

我有几台运行Octane渲染器的计算机。一个是";"大师";其他的是";奴隶";。当Master无法访问时,Slaves无法渲染,所以我想通过为这种情况提供一个小批量脚本来节省资金和环境。

我的解决方案是在奴隶身上有一个批脚本,每5分钟ping一次大师,以检查是否有连接。当ping失败时,它会再次尝试3次,如果每次从机关闭时都失败。

我从这里的另一篇文章中得到了这个脚本,我对它做了一些小编辑:在网络连接丢失时重新启动电脑的批处理文件,但它不适合我的需要。当我将ping ip设置为Master的ip时,它会显示我的连接处于活动状态。当试图在命令提示符中ping Master时;目的主机不可达";。如有任何帮助,我们将不胜感激!

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: Shutdown PC in 60 seconds.  Press any key to abort.
shutdown -s -t 60 -f
pause > nul
shutdown -a
goto end
:end ```

对于如此简单的东西,您的代码相当大。首先,您在ipv4上设置的地址不是正确的地址!您可以访问我的存储库,了解如何在此处获取您的IPv4代码。

获得您的IPv4地址后,添加到您的代码中:

ping %ipv4% -n 9 | findstr /i "unreach">filex.txt
set /p stop=<filex.txt
if "%stop%" neq "" (
:: put here your shutdown preferences.
)

如果你想要像你正在搜索的东西:

@echo off
:verif
set points=
echo INFO: Checking connection, please hang tight for a second...
ping %ipv4% -n 3 | findstr /i "unreach">filex.txt
set /p stop=<filex.txt
if "%stop%" neq "" set /a points=%points%+1
del /q /f filex.txt
ping %ipv4% -n 3 | findstr /i "unreach">filed.txt
set /p stopd=<filed.txt
if "%stopd%" neq "" set /a points=%points%+1
:: Add points as you want.
if "%points%" geq "2" (
set shutdown=y
)
if "%shutdown%" equ "y" (
echo ERROR: You do not have an active connection.
shutdown -s -t 60 -f
echo INFO: Shutdown PC in 60 seconds.  Press any key to abort.
pause>nul
shutdown -a
goto verif
)
echo INFO: You have an active connection.
goto verif

希望这能有所帮助,
K

相关内容

最新更新