电源管理批处理文件在另一台电脑关闭/休眠/断开连接时关闭电脑

问题描述

这是我在这里的第一篇文章,所以请原谅我的任何错误

我有几台运行 Octane 渲染器的计算机。一个是“主人”,另一个是“奴隶”。当 Master 无法访问时,Slaves 无法渲染,所以我想通过在发生这种情况时使用一个小批处理脚本来节省资金和环境。

我对解决方案的想法是在 Slaves 上有一个批处理脚本,每 5 分钟 ping Master 以检查是否有连接。当 ping 失败时,它会再次尝试 3 次,如果每次 Slave 关闭都失败。

我从这里的另一篇文章中得到了这个脚本,我对它做了一些小的修改Batch File to reboot pc on network connectivity loss 但它不能满足我的需要。当我将 ping ip 设置为 Master 的 ip 运行它时,它说我的连接处于活动状态。在命令提示符中尝试 ping 主机时,我得到“目标主机无法访问”。任何帮助将不胜感激!

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 代码 here

获得您的 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.

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...