0

this is my first post here so please excuse me for getting anything wrong.

I have a few computers that run Octane renderer. One is the "Master" and the others are "Slaves". When the Master is unreachable the Slaves can't render so I'd like to save money and the environment by having a small batch script for when this happens.

My thinking to a solution was to have a batch script on the Slaves to ping the Master every 5 mins to check there is a connection. When the ping fails it tries again 3 times and if it fails every time the Slave shuts down.

I've got this script from another post here that I did minor edits to : Batch File to reboot pc on network connectivity loss but it doesn't work for what I need. When I run it with the ping ip set to the Master's ip it says my connection is active. When attempting to ping the Master in a command prompt I get "destination host unreachable". Any help would be greatly appreciated!

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 ```
Gideon
  • 1
  • 1
  • `1.1.1.1` is the IPv4 Address for Cloudflare, not your Master! – Compo Apr 22 '21 at 15:10
  • Hi, thanks for the reply. I do know that IP address is for Cloudflare, when I change that address to the IPv4 address for the master which is on the local network the script does not work for me. – Gideon Apr 22 '21 at 16:52

1 Answers1

0

Your code is quite large for something so simple. First, the address you set on ipv4 is not the correct address! You can access my repository to read how to acquire your IPv4 code here.

After obtain your IPv4 address, add to your code:

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

If you want something like what you are searching for:

@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

Hope this helps,
K.