Linux Howto Use Wake On Lan feature to send a command to HTPC
#1
Bug 
I have been suffering from freezes every now and then from using beta addons in XBMC and as i have no other input devices for my HTPC i decided to see if it was possible to use the Wake On Lan feature available on most smartphone remotes to send a command to the HTPC

Wake On Lan is the feature that sends a "Magic Packet" to the mac address that you define telling the system to turn on if you enable it in the bios.

I started looking at the tcpdump output and seeing how to capture the input when i came across magic_shutdown http://blog.applegrew.com/2008/02/magic_...ic-packet/

I am posting my findings and slight alterations to the script here in case anyone finds it useful - I call it cyclexbmc as i use it to kill xbmc.bin when it freezes

The Script ( to be place in /opt/ )
Code:
sudo nano /opt/cyclexbmc

Then paste the code below into the new nano window

Code:
#!/bin/bash
#Author: Stuart
#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 cycling XbMc"
rm -f $1
pkill -9 xbmc.bin
fi
done

Press Ctrl+x to Exit and press Y to save changes

Next we need to add the start up script

Code:
sudo nano /etc/init.d/cyclexbmc

Then paste the code below into the new nano window

Code:
#!/bin/bash
#Author:AppleGrew
#License:GPL version 3
SCRIPT="/opt/cyclexbmc"
PID_FILE="/var/run/cyclexbmc.pid"
case "$1" in
start)
test -f "$PID_FILE" && echo "Already Running..." && exit 1
"$SCRIPT" "$PID_FILE"
echo "Started"
;;
stop)
pid=`cat "$PID_FILE"`
tcpPid=`pgrep -P $pid tcpdump`
kill -9 $pid
kill -2 $tcpPid
if [ -f "$PID_FILE" ] && ! ps -p $pid >/dev/null
then
rm -f "$PID_FILE"
else
echo "Failed to delete pid file. Maybe its already deleted."
fi
echo "Stopped"
;;
esac

Press Ctrl+x to Exit and press Y to save changes

Now make both scripts executable

Code:
sudo chmod a+x /opt/cyclexbmc

and

Code:
sudo chmod a+x /etc/init.d/cyclexbmc

Now to make the init script start at boot

Change Directory to /etc/rc2.d/

Code:
cd /etc/rc2.d/

and make a link to the init script in the rc2.d folder

Code:
sudo ln -s ../init.d/cyclexbmc S99cyclexbmc

reboot your machine and try WoL to restart xbmc.bin

I use Constellation as my remote on the iPhone - In host settings go to the WOL MAC: field and place your mac address in there use "-" instead of ":" i.e a mac address of 00:a0:d1:ae:01:34 becomes 00-a0-d1-ae-01-34

To find out your Mac Address use ifconfig and note down the HWaddr for the Adapter your using

Final note if you want the script to do something other than restart xbmc or you use a different command to cycle do

Code:
sudo nano /opt/cyclexbmc

at the bottom you will see

Code:
if [[ "$pckt_data" == "$pckt_expect" ]]
then
echo "Matched! Received Magic packet cycling XbMc"
rm -f $1
pkill -9 xbmc.bin
fi

change the line pkill -9 xbmc to whatever bash command you want to execute be sure to restart the init script after modifying cyclexbmc or just reboot

Hope this post is helpful to someone and in keeping with forum rules Smile

Jon
Reply

Logout Mark Read Team Forum Stats Members Help
Howto Use Wake On Lan feature to send a command to HTPC0