0

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'

This tells 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. bye):

$ socat STDOUT UDP4-DATAGRAM:255.255.255.255:6666,broadcast
bye

This will broadcast the message (in this case bye), 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.

  • I wrote a trivial systemd service file that can be installed to run the first of the above socat commands above on a machine at startup. See gist.github.com/george-hawkins/21f98e4061fed8d4bb3d9183093c0726 – George Hawkins Apr 19 '17 at 14:10
  • 1
    We're happy that you have answered the question yourself, however, in order to make that apparent here on this site, you should post the ANSWER in the Answer section below, and not simply include it in your Question. Please edit your post to meet these criteria. There is an enforced delay between posting an answer to your own question, and actually being able to mark it as the correct answer, but this method allows it to be clear what the Question is, and then what the Answer to that question is. – music2myear Apr 19 '17 at 17:42
  • I was hoping for a better answer, i.e. I was showing my current approach and asking if there was a better one based on an established protocol like SNMP or Bonjour. But I can split it into an answer (after the enforced delay is over) and see how it does in voting. – George Hawkins Apr 19 '17 at 19:58
  • Ok. Then there was some clarity lacking in your post. If you want a better answer, that's good too. Perhaps using some formatting and breaking your post up with headings "This Is What I'm Doing Now", "This Is What It Doesn't Do", and "This Is What I'd Like It To Do", or similar to clarify your request would make it easier to tell what's going on. – music2myear Apr 19 '17 at 20:47
0

only shuts down if e.g. a long random number that should only be known to the interested parties is seen.

Well, if you're fine with that level of coordination, then just have the master host SSH to all other servers and run the shutdown command that way. The servers only need to know a long random number, i.e. the master's public key.

(The corresponding private key is not limited to a single master; you can copy the ~/.ssh/id_* file wherever you need.)

Bonjour/Avahi is purely a discovery mechanism and does not support broadcasting commands, but you can use it here to list all machines in the subnet (either by the generic "workstation" service, or by a custom-defined one).

|improve this answer|||||
  • Thanks for your answer - I already use ssh (with RSA keys) for routine interactions between the master and individual non-master / slave machines. However see my comment on @jehutyy's answer. The shutdown situation is a one-to-many command and the socat approach avoids looping over all the slaves. I'd thought maybe SNMP, Bonjour or something else might already provide a less ad-hoc approach to this (I get your remark re Bonjour). But perhaps socat is the best approach for my setup and if I need more I should look at some full blown resource management system. – George Hawkins Apr 19 '17 at 20:16
0

Since you have to execute a command on "non-master" I assume there is some sort of coordination between master and non-master. So you could use ssh with ssh key-pair authentication to execute command remotely like this :

ssh -i /path/to/pubkey @server 'shutdown -H now' 
|improve this answer|||||
  • This is actually the approach I used before the socat one outlined above. However there's one master and many non-master or slave machines - so one has to loop over all the slaves with this approach. And at any given time some proportion of slaves will already be offline - so one sees many ssh failures (not a big deal). But I thought Bonjour or SNMP might be a way to go - one could query to see who was advertising a "shutdown" service and then ask the machines that responded to do so. – George Hawkins Apr 19 '17 at 20:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.