I have a group of linux servers on my subnet that I want to be able to remotely shutdown from a master machine. I don't want the non-master machines to have to coordinate with the master in advance, i.e. know its IP address etc. So something based around broadcast or multicast messages would seem an obvious approach.
I've already implemented a solution with
socat but I was wondering if there was a better less ad-hoc solution e.g. using one of the established multicast aware services like SNMP or Bonjour?
My solution is as follows. On all the non-master machines I run:
$ socat UDP4-RECVFROM:6666,broadcast,fork SYSTEM:'hostname; shutdown now'
socat to listen for broadcast messages on port 6666 and when it receives one it echoes back the hostname and shuts down the machine.
Now any machine on the same subnet can shutdown all machines running this with the following command (it waits for input so enter anything, e.g.
$ socat STDOUT UDP4-DATAGRAM:255.255.255.255:6666,broadcast bye
This will broadcast the message (in this case
socat will then output the hostnames echoed back by all the machines that were listening for broadcast messages on port 6666.
There's obviously no security with this approach - if any machine on the same subnet broadcasts anything to port 6666 then the systems will shutdown. One could add a simple extra layer - instead of just running
hostname; shutdown now one could run a script that actually checks what was broadcast and only shuts down if e.g. a long random number that should only be known to the interested parties is seen.