0

I have a LAN with 4 PCs and one Synology server. I wish that every time a computer is turned off, it runs a batch (from windows pc) and see if other PCs are on, - if any is on do nothing, - otherwise runs a command that shuts down the server.

Here is what I have at the moment:

@echo off PING 192.168.1.10 IF %ERRORLEVEL% EQU 1 plink root@192.168.1.10 -pw MYPASSWORD poweroff

But I would like to do something like: https://i.stack.imgur.com/fCPBz.png

THANK YOU!!

user37547
  • 11
  • 1
  • 3

4 Answers4

1

No need to do it from client-side.

I would do it on server-side: check if at least one client is up, if not then shutdown (basically a one-liner):

(ping -4 Client1 & ping -4 Client2 & ping -4 Client3 & ping -4 Client4) |find "TTL=" >nul || shutdown -s -t 60 -f -c "Shutdown because all clients are down"

Create a scheduled task on the server that runs this script - let's say all 5 minutes. The timeout gives you enough time to abort the shutdown (shutdown -a) in case you are working on the server.

Stephan
  • 53,940
  • 10
  • 58
  • 91
0


first you should schedule a shutdown batch.. follow these steps to run the batch when you shut down..
https://technet.microsoft.com/en-us/magazine/dd630947.aspx
The batch looks fine.. but if you want to do nothing in the first case and shutdown in the second case use it like this:

@echo off 
PING 192.168.1.10
IF %ERRORLEVEL% EQU 1 plink root@192.168.1.10 -pw MYPASSWORD shutdown -s -t 00

So the statement "do nothing" is deleted and the poweroff command is shutdown -s -t 00

user3379482
  • 557
  • 1
  • 10
  • 21
  • thank you for the answer, but how can I see if the other pcs in the lan are on? – user37547 Feb 03 '15 at 16:54
  • My pleasure.. check this url to show how to check if they are online http://stackoverflow.com/questions/3050898/how-to-check-if-ping-responded-or-not-in-a-batch-file – user3379482 Feb 03 '15 at 17:00
  • ok now how can do "if" "then" else with other pc? I've added some other line in the description to better understand what i would like to do ;) thank you for your time!! – user37547 Feb 03 '15 at 17:05
  • This is a bad way to check if ping was successful or failed. It'd be better to `ping host | find "TTL=" >NUL && success || fail`. See [this answer](http://stackoverflow.com/a/23720500/2861476) for a more thorough explanation, and [this answer](http://stackoverflow.com/a/27787239/1683264) for a less complicated demonstration. – rojo Feb 03 '15 at 17:18
0
for %%a in (pc-1 pc-2 pc-3 pc-4) do if /i "%computername%" neq "%%a" ping %%a&if not errorlevel 1 goto :eof
echo shut-down-the-server-code

That way, you ping until you find a PC that's on, skipping your own (which is obviously on). If any are found on, just terminate the batch by jumping to end-of-file. If all are off (other than yours, obviously) then shut down the server and shut down your PC.

Magoo
  • 77,302
  • 8
  • 62
  • 84
  • `ping host` can set errorlevel=0 on `Reply from (local IP): destination host unreachable.` – rojo Feb 03 '15 at 19:02
  • @rojo - just using OP's methodology. Substitute superior methodology if required... – Magoo Feb 03 '15 at 19:07
0

http://ss64*org/viewtopic.php?pid=8188

http://stackoverflow*com/questions/28304099/batch-script-to-ping-other-pc-in-my-lan-and-shutdown-together-last-online-pc

http://wwwmsfnorg/board/topic/173438-shutdown-last-running-pc-on-the-lan-with-server/

http://forum_synology*com/enu/viewtopic.php?f=145&t=96609

http://www_dostips*com/forum/viewtopic.php?f=3&t=6245

I think last one will be more easy to use thank you for the answer!

@echo off
set "flag="
ping 192.168.1.1 -n 2  |find /i "TTL=" >nul && set flag=1
ping 192.168.1.2 -n 2  |find /i "TTL=" >nul && set flag=1
ping 192.168.1.3 -n 2  |find /i "TTL=" >nul && set flag=1
ping 192.168.1.4 -n 2  |find /i "TTL=" >nul && set flag=1
if not defined flag echo shutdown server
zanazzzz
  • 1
  • 1