Remotely shutdown/reboot Linux boxes without SSH? [closed]
Asked Answered
D

2

12

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.

Damnation answered 16/12, 2011 at 0:21 Comment(5)
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: 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'.Sceptre
"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?Farland
Well there's always xt_SYSRQ ;-)Maritime
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.Semicentennial
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 @Sceptre said, but is there an existing one around?Damnation
G
16

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.

Glooming answered 19/5, 2015 at 14:53 Comment(2)
When I install xtables-addons-common and iptables the path /sys/module/xt_SYSRQ still does not exist. Am I missing something?Guidepost
To answer my own question, run sudo modprobe xt_SYSRQ.Guidepost
M
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.

Mancy answered 17/12, 2011 at 6:19 Comment(2)
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 ;-)Connally
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.Mancy

© 2022 - 2024 — McMap. All rights reserved.