Remote shutdown through WOL mechanism?

I am not sure this is the right place to post this; my apologies in advance if it is not the right place.

I am running openSuSE 13.1, and I have wake-on-lan working. From what I have read elsewhere, it also sounds like it is possible to shutdown a computer remotely via a similar mechanism. The magic packet utility that I used to WOL my 13.1 installation also has an option to send a shutdown command via what looks like the same mechanism. However, when I do this, the 13.1 installation does not shut down.

Is it possible to set 13.1 so that it will shutdown when this shutdown command is received via the NIC?

Thanks.

No idea, but if the box is up why not send it a shutdown command via other
means? For example:


ssh root@remoteHostHere 'shutdown -h now'


Good luck.

If you find this post helpful and are logged into the web interface,
show your appreciation and click on the star below…

On 2014-10-05 06:56, wiyosaya wrote:
>
> I am not sure this is the right place to post this; my apologies in
> advance if it is not the right place.
>
> I am running openSuSE 13.1, and I have wake-on-lan working. From what I
> have read elsewhere, it also sounds like it is possible to shutdown a
> computer remotely via a similar mechanism. The magic packet utility that
> I used to WOL my 13.1 installation also has an option to send a shutdown
> command via what looks like the same mechanism. However, when I do this,
> the 13.1 installation does not shut down.

That thing would not work via hardware, as the WOL does. It has to be
necessarily handled by the operating system, which in Linux surely means
installing something and enabling it.

And no, don’t ask me what that hing would be, because I don’t know if it
even exists.

If you have some documentation for the shutdown thing that you use,
perhaps somebody can find the corresponding Linux utility.


Cheers / Saludos,

Carlos E. R.
(from 13.1 x86_64 “Bottle” at Telcontar)

Hi
And if your really wanting to use the magic packet, have a read here;
http://blog.applegrew.com/2008/02/magic_shutdown-shutdown-computer-remotely-using-magic-packet/

It would need to be modified for systemd (as in create a service) as well of the power mechanism if your using something other than GNOME…

On 2014-10-05 18:56, malcolmlewis wrote:
>
> Hi
> And if your really wanting to use the magic packet, have a read here;
> http://tinyurl.com/oruujny

The script from that page displays awfully here:


Get magic_shutdown‘s code
License: GNU Public License version 3.
<span class="com">#!/bin/bash</span><span class="pln">
</span><span class="com">#Author:AppleGrew</span><span class="pln">
</span><span class="com">#License:GPL version 3</span><span class="pln">
listenPort</span><span class="pun">=</span><span
class="lit">9</span><span class="pln">
....

Ie, it displays the HTML code for something, instead of that something.

I see a flash (blocked) item. I unblock it, nothing. I reload the page,
then I do see the intended code… for some seconds. After some seconds,
it “obfuscates” again.


Cheers / Saludos,

Carlos E. R.
(from 13.1 x86_64 “Bottle” at Telcontar)

Thanks for the replies.

Scrolling down on that page, there is another version of the script that is supposed to be better and it is also readable.

I am not sure what I will do at this point, but either the ssh approach or this approach seems valid.

Perhaps another approach might be to watch for that packet as a rule in my IP Tables script, log it to a special file that is being watched by some process similar to the script on the above page, then when that is seen, shutdown.

Thanks again.

There is also another version of the script here - http://forum.xbmc.org/printthread.php?tid=125563 that has a variation on its use (and is also readable).

For anyone interested, I came up with a script that shuts down an openSuSE system when a WOL packet is received by a monitored interface. It is based on the script in this thread.

I had to add some extra parsing to remove extraneous characters from both ifconfig and tcpdump. I thought that those characters would be removed by the default script. I did not investigate the reason, however, I anticipate that it was due to some differences in implementation of sed and awk (etc.) between openSuSE and the Linux variant the original author was running. I have been using this for several weeks now, and it works well. The IT Director, (my wife) loves it! lol!

#! /bin/sh
#
# Author: Matthew Z.
# License:GPL version 3
# based on the script at http://forum.kodi.tv/printthread.php?tid=125563
#
# /etc/init.d/magicshutdown
#
### BEGIN INIT INFO
# Provides:      magicshutdown
# Required-Start: network
# Required-Stop:
# Default-Start:  2 3 5
# Default-Stop:
# Description:    Listens for WOL magic packet and then shuts down the server
### END INIT INFO

IFACE=enp3s0
NAME=magicshutdown

# Shell functions sourced from /etc/rc.status:
#      rc_check         check and set local and overall rc status
#      rc_status        check and set local and overall rc status
#      rc_status -v     ditto but be verbose in local rc status
#      rc_status -v -r  ditto and clear the local rc status
#      rc_failed        set local and overall rc status to failed
#      rc_failed <num>  set local and overall rc status to <num><num>
#      rc_reset         clear local rc status (overall remains)
#      rc_exit          exit appropriate to overall rc status
. /etc/rc.status

# First reset status of this service
rc_reset

case "$1" in
    start)

#Forking to daemonize…
if  "$2" != "forked" ]]
then
    echo “Forking $0…”
    $0 $1 forked &
    echo “Forked.”
    exit 0
fi

#Creating pid file
ppid=$$
echo $ppid >”$1″

echo “Started”
mac=`ifconfig $IFACE|head -n1|sed -e 's/.*HWaddr \([0-9:a-fA-F]*\)/\1/g' -e 's/://g'`
pckt_expect=`echo $mac $mac $mac $mac $mac $mac $mac $mac $mac $mac $mac $mac $mac $mac $mac $mac 00|sed 's/ //g'|tr 'A-Z' 'a-z'`
while `true`
do
pckt_recd=`tcpdump -i $IFACE -s 0 -x -c 1 \( \(ether dst $mac and not ip and not arp and not rarp\) or \(udp port 7\) \)`
if  $? != 0 ]]
then
echo “tcpdump returned error.”
exit 1
fi
pckt_data=`echo $pckt_recd | \
grep '0x[0-9]*:'| \
tr 'A-Z' 'a-z'| \
sed 's/ 	]//g'| \
sed 's/0x[0-9]*:\([0-9a-f]*\)/\1/g'| \
sed 's/0x[0-9]*:\([0-9a-f]*\)/\1/g'| \
sed 's/0x[0-9]*:\([0-9a-f]*\)/\1/g'| \
sed 's/0x[0-9]*:\([0-9a-f]*\)/\1/g'| \
tr -d '
\r' | \
awk -F 'ffffffffffff' '{print $2}'`
if  $pckt_data == $pckt_expect ]]
then
echo “Matched! Received Magic packet shutting down…”
rm -f $1
/sbin/poweroff
exit 0
fi
done

echo “EXITED”
exit 0    # Remember status and be verbose
    rc_status -v
    ;;
    stop)
        kill -1 `ps -e|grep $NAME|cut -d ' ' -f2`
        rc_status -v
    rc_status -v
    ;;
    restart)
    ## Stop the service and regardless of whether it was
    ## running or not, start it again.
    $0 stop
    $0 start

    # Remember status and be quiet
    rc_status
    ;;
    *)
    echo "Usage: $0 {start|stop|restart}"
    exit 1
    ;;
esac
rc_exit

Well done. Thanks for sharing.

JFYI, that script will not work on systems if /bin/sh does not point to /bin/bash , for now that script is safe on openSUSE and on any distro that symlink /bin/sh to /bin/bash but when the devs decided to use another shell instead of bash to provide sh shell then you will be in a world of hurt and surprises lol!

According to POSIX sh manpages does not exists…

http://pubs.opengroup.org/onlinepubs/007904975/utilities/xcu_chap02.html

Try this too.

mac=$(ifconfig "$IFACE" | awk '/HWaddr/{gsub(/:/,"");print $NF;exit}')

Any way, have fun with your task :slight_smile:

On 2014-11-01 21:26, wiyosaya wrote:
>
> For anyone interested, I came up with a script that shuts down an
> openSuSE system when a WOL packet is received by a monitored interface.
> It is based on ‘the script in this thread’
> (http://forum.kodi.tv/printthread.php?tid=125563).
>
> I had to add some extra parsing to remove extraneous characters from
> both ifconfig and tcpdump. I thought that those characters would be
> removed by the default script. I did not investigate the reason,
> however, I anticipate that it was due to some differences in
> implementation of sed and awk (etc.) between openSuSE and the Linux
> variant the original author was running. I have been using this for
> several weeks now, and it works well. The IT Director, (my wife) loves
> it! lol!

Interesting. Thanks for sharing.

An idea for food: hook on xinetd. It already listens…


Cheers / Saludos,

Carlos E. R.
(from 13.1 x86_64 “Bottle” at Telcontar)