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.
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.
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
#!/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
#!/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
This solution works only if your bricked computer is able to handle TCP connections.
#!/bin/bash
apt-get install -qq sysrqd
echo "yolo" > /etc/sysrqd.secret
service sysrqd restart
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.