magic_shutdown: Shutdown computer remotely using Magic Packet

You are currently browsing comments. If you would like to return to the full story, you can read the full entry here: “magic_shutdown: Shutdown computer remotely using Magic Packet”.

Comments
3 Responses to “magic_shutdown: Shutdown computer remotely using Magic Packet”
  1. Alex says:

    And tool for lazy – Wake-On-LAN over Internet with scheduling – http://www.rshut.com/wol

  2. StuartNo Gravatar says:

    Hi,
    Just what I was looking for :O) saved me some work, but I did find I needed to make some changes. The tcpdump filter wouldn’t work for magic packet sent over ethernet only, i.e. not UDP, so I changed the filter to capture both. Also The parsing didn’t work as it seems the MAC address is not repeated 16 times. Lastly, the “ps -p” didn’t work on my platform, so I made a more portable version checking for the PID. Attached is the updated script if you want it.

    I forgot, I also changed the packet parsing to break the packet up using the ffffffffffff in the packet as the two types of packet are of differnt lengths if using UDP or not, and also added a statement to determine the “main” ethernet interface of the machine so it would automatically work for devices other than eth0.

    Thanks again…
    Stuart/

    #!/bin/sh
    #Original Author:AppleGrew
    #License:GPL version 3

    #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”
    interface=`route -n | grep “^0.0.0.0″ | awk -F ” ” ‘{print $8}’`
    mac=`ifconfig “$interface”|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”|sed ‘s/ //g’|tr ‘A-Z’ ‘a-z’`
    while `true`
    do
    pckt_data=`tcpdump -i “$interface” -s 0 -x -c 1 \( \(ether dst “$mac” and not ip and not arp and not rarp\) or \(udp port 9\) \)`
    if [[ $? != 0 ]]
    then
    echo “tcpdump returned error.”
    exit 1
    fi
    pckt_data=`echo “$pckt_data” | \
    grep ’0x[0-9]*:’| \
    tr ‘A-Z’ ‘a-z’| \
    sed ‘s/[ \t]//g’| \
    sed ‘s/0x[0-9]*:\([0-9a-f]*\)/\1/g’| \
    tr -d ‘\n\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

  3. Apple GrewNo Gravatar says:

    That’s great Stuart. I have added your code to my post.

Leave A Comment

Page optimized by WP Minify WordPress Plugin