Batch listen for ping to shutdown

481 views Asked by At

I'm trying to make a batch file which is waiting for 6 packets ping from a certain ip from the network. When the machine receives the 6 echo requests should shutdown automatically. I will make this batch file to run at startup as a service, i dont need help for this part. Can anyone help me with the listening part ?

1

There are 1 answers

0
mojo On

As an alternative, why not create a file in a special place (perhaps even with special content) that the "listening" script constantly looks for, and as soon as it exists, deletes the file and shuts the machine down?

This could at least be done in batch.

SETLOCAL
SET "HANDSHAKE=%WINDIR%\System32\SPECIAL\SHUTDOWN_NOW"
:LOOP
IF EXIST "%HANDSHAKE%" (
    DEL /Q "%HANDSHAKE%"
    shutdown -s -f -t 0
)
PowerShell -NoProfile -Command "Sleep(60);"
GOTO :LOOP

Windows has no native sleep command. An alternative strategy that others have thought of that have less overhead than PowerShell (though, since it's just sleeping, the overhead probably doesn't matter) is to ping the localhost adapter times (e.g. ping -n 60 127.0.0.1).

Of course, the whole script could be written in PowerShell, but then you would have to fiddle with ExecutionPolicy and other things.