10

I need to remotely shutdown and reboot Linux (Ubuntu) machines without logging into them (otherwise simple commands can do the job). The machines are just cheap PCs so there are no special power management hardware installed (though they can wake-on-lan). Is there some sort of "power management server" software that I can install on those boxes, which listens to remote requests for reboot/shutdown and acts accordingly? Of course it would be nice if it requires some authentication (password) in order to respond to the requests.

jasxun
  • 551
  • 1
  • 3
  • 10
  • I'm not aware of anything existing. But also not sure why you don't want to just login over ssh. You could use python to do the login and script some commands, like shown here: http://www.goldb.org/sshpython.html But if you really don't want to use ssh, you could write a simple python script to open a socket and listen for a request, then issue a command line 'reboot'. – TJD Dec 16 '11 at 01:42
  • 1
    "Of course it would be nice if it requires some authentication " -- b-b-b-but if you want a lot of `ssh`'s feature set, and you can install software, why not just install `ssh`? – Brian Cain Dec 16 '11 at 01:45
  • 3
    Well there's always xt_SYSRQ ;-) – jørgensen Dec 16 '11 at 02:25
  • I agree with Brian Cain. What's wrong with ssh? It like saying you want to drive in a hiway but don't want to use a car. – mike jones Dec 16 '11 at 05:47
  • I want to remotely control **lots of machines**, logging into them one by one is not the way to go. I think if there's such a "power management server software", I can just broadcast a request to all machines and do the trick. It could be just "a simple python script to open a socket and listen for a request" as @TJD said, but is there an existing one around? – jasxun Dec 16 '11 at 17:35

2 Answers2

12

As pointed out by jørgensen, you can use SYSRQ (http://en.wikipedia.org/wiki/Magic_SysRq_key), an API directly talking to the kernel.

Beware, these are quite hardcore and may harm your hardware. It takes the time of a single UDP packet transfer to reboot. Boom. We only use it on live diskless computers.

1. xt_SYSRQ (iptables modules, kernel)

There is xt_SYSRQ, one of the iptables modules provided by xtables-addons-common : http://manpages.ubuntu.com/manpages/oneiric/man8/xtables-addons.8.html

Installing on debian

#!/bin/bash
apt-get install -qq xtables-addons-common iptables
echo -n "yolo" >/sys/module/xt_SYSRQ/parameters/password
iptables -A INPUT -p udp --dport 9 -j SYSRQ

Shotgun reboot

#!/bin/bash
sysrq_key="sub"  # the SysRq key(s), Sync, Unmount, reBoot
password="yolo"
seqno="$(date +%s)"
salt="$(dd bs=12 count=1 if=/dev/urandom 2>/dev/null | openssl enc -base64)"
ipaddr="$1"
req="$sysrq_key,$seqno,$salt"
req="$req,$(echo -n "$req,$ipaddr,$password" | sha1sum | cut -c1-40)"
echo "$req" | socat stdin udp-sendto:$ipaddr:9

2. sysrqd (tcp 4094 listening daemon, userland)

This solution works only if your bricked computer is able to handle TCP connections.

Installing on debian

#!/bin/bash
apt-get install -qq sysrqd
echo "yolo" > /etc/sysrqd.secret
service sysrqd restart

Shutgun reboot

I made a script, https://gist.github.com/qolund/1470beaa1a63e034025d but its just a TCP connexion on port 4094. You need to send the password and the commands,

# telnet 172.16.42.180 4094
Trying 172.16.42.180...
Connected to 172.16.42.180.
Escape character is '^]'.
sysrqd password: nope
Go away!
Connection closed by foreign host.
# telnet 172.16.42.180 4094
Trying 172.16.42.180...
Connected to 172.16.42.180.
Escape character is '^]'.
sysrqd password: yolo
sysrq> sub
[..]

The connection isn't properly closed, because the 'b' reboot command is too fast, the computer is already rebooting.

Nope
  • 797
  • 1
  • 6
  • 15
4

A few options:

This tools are not exactly to shutdown machines (but they can do it), they are configuration management frameworks to administer a lots of machines, they can handle configuration changes, package installs and updates, and run all the commands you want, in one machine, in a set of machines, or in the whole network.

Pablo Castellazzi
  • 4,014
  • 20
  • 20
  • Puppet is actually a really good idea! YOu would have to have some kind of crontab looking at a puppet config value regularly, ( a few clock cycles of CPU time.) Just reconfigure the value based on some criteria, puppet broadcasts it, bada bing, bada boom! SHUTDOWN! PS, you would have to CHANGE that configuration upon any restarts by any program or it would immediately shut down once it was finished booting ;-) – Dennis Nov 13 '12 at 03:53
  • Older versions of puppet included a tool to run a command on managed nodes. This functionality is now handled by a separate tool called MCollective. – Pablo Castellazzi Jan 25 '13 at 16:42