Active Topics

 


Reply
Thread Tools
Posts: 840 | Thanked: 823 times | Joined on Nov 2009
#1
This is a tutorial to turn on and access/control a pc from your N900 (or any other linux machine really).
It may seem complicated at first but it is a small introduction to using linux and a very powerful tool called shell scripts. Most of the things here you may already know. I have tried to explain steps and terms as clearly as I can (almost bordering on patronising but not intended to be) for new linux and N900 users. Having said that I'd like to put a disclaimer that this is not for the faint of heart. Be very careful what you do with your phone and do it at your own risk.

Turn on a PC. WOL
Wake on lan (WOL) is a method to turn on a machine with a "signal" known as a magic packet sent from another device on a network.
Both the machine and the network need to be setup for this, this is not discussed here. I recommend you google for "WOL BIOS" "WOL portforwarding" to setup the machine and network.


There are two ways to send the signal. You only need to use one or the other.

1) get the N900 to send it
2) Use the N900 to tell a router, NAS device or another PC to send it.

1) sending it from the N900 directly


This is probably the easier method. For people who want to wake many machines on their lan and have a router capable of ssh and WOL (eg DD-WRT routers) I recommend method two.

install the pygtkeditor app, this is what you use to write scripts. (applications menu -> more -> app manager -> download ->all)
open an x terminal on the N900 (applications menu -> more -> x terminal)

type the following and press enter to create a directory (folder)
Code:
mkdir ./MyScripts
to open the editor type pygt then press ctrl+I. see what happened? it types the rest for you. don't press enter yet, complete the command:
Code:
pygtkeditor /home/user/MyScripts/wakeonlan.py
don't worry, it will complain about not finding the file. click away.
copy paste the script below into the editor. for a guide to copy paste from the browser look at this post and use the stylus not your finger
http://talk.maemo.org/showpost.php?p=443893&postcount=8

Code:
#!/usr/bin/python

# Wake-On-LAN
#
# Copyright (C) 2002 by Micro Systems Marc Balmer
# Written by Marc Balmer, marc@msys.ch, http://www.msys.ch/
# This code is free software under the GPL
# minor edit by Cue from maemo talk for command line arguments.

import struct, socket, sys

def WakeOnLan(ethernet_address):

  # Construct a six-byte hardware address

  addr_byte = ethernet_address.split(':')
  hw_addr = struct.pack('BBBBBB', int(addr_byte[0], 16),
    int(addr_byte[1], 16),
    int(addr_byte[2], 16),
    int(addr_byte[3], 16),
    int(addr_byte[4], 16),
    int(addr_byte[5], 16))

  # Build the Wake-On-LAN "Magic Packet"...

  msg = '\xff' * 6 + hw_addr * 16

  # ...and send it to the broadcast address using UDP

  s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
  s.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
  s.sendto(msg, ('<broadcast>', 9))
  s.close()

#use
WakeOnLan(sys.argv[1])
now save it by clicking on the menu bar (wakeonlan.py at the top of the screen). click "save as" then "save" (don't change the directory or filename). close the editor to get back to the terminal

at the terminal use this command so that you can execute the script
Code:
chmod a+x /home/user/MyScripts/wakeonlan.py
That's it, now you have a script you can use to turn on your PC. To turn on a PC type the following at the terminal:
Code:
/home/user/MyScripts/wakeonlan.py "YOUR PC MAC ADDRESS"
where you obviously replace "YOUR PC MAC ADDRESS" with the mac address of the PC you want to turn on.
there is a way to make most of the directory commands here shorter if you want to use the command often, but we'll learn how to create a shortcut on the desktop further down the tutorial anyway.


2) telling a router to send the wake signal
if you followed the step above (sending it from the N900 directly) you don't need to do this section (section 2), you can do both and nothing wrong will happen but there really is no need to do both. if you have network knowledge, a lot of PCs to wake from a WAN, or an always on DDWRT-router/low power PC I'd recommend this over step 1. if you use a DDWRT router first make sure you enable ssh on the router from both the services and managment tab.

Now the script is a lot easier and waking more than one machine in your LAN from a WAN is easier too.

like in section 1) above instead of
Code:
pygtkeditor /home/user/MyScripts/wakeonlan.py
use
Code:
pygtkeditor /home/user/MyScripts/wakeonlan.sh
notice the sh at the end in place of py.
Copy paste the short script below and save the file.

Code:
#!/bin/sh
# Indirect Wake on Lan
# By Cue from maemo talk
wol()
{
    ssh $1@$2 /usr/sbin/wol -i $3 $4
}

#use
wol "YOUR_ROUTER_or_PC_USERNAME" "YOUR_ROUTER_IP_or_HOSTNAME" "YOUR_BROADCAST_ADDRESS" $1
replace "YOUR_ROUTER_or_PC_USERNAME" "YOUR_ROUTER_IP_or_HOSTNAME" "YOUR_BROADCAST_ADDRESS" with the appropriate values which I think for a DDWRT router is usually:
Code:
wol "root" "192.168.1.1" "192.168.1.255" $1
don't change the $1 at the end.
if you want to wake from a WAN (usually from outside your house) instead of "192.168.1.1" put your ISP ip or a DYNDNS address and set up port forwarding.

Code:
chmod a+x /home/user/MyScripts/wakeonlan.sh
wake a pc from the terminal in the same way as section 1
Code:
/home/user/MyScripts/wakeonlan.sh "YOUR PC MAC ADDRESS"
what this essentially does is tell the router (or another PC) to run a wol "program" or script (a script like in step one) instead of the phone itself. This saves some trouble with port forwarding if you want to wake more than one machine from a WAN.

3) turn on AND ACCESS a PC
OK so now we can turn on a PC. This is a guide to access it.
there are two ways to control a PC used here. SSH or VNC. I recommend SSH (and linux ofcourse).

On the N900 install both vncviewer and OpenSSH client so you can do both (applications menu -> more -> app manager -> download ->all)
if your PC is running Windows: install realvnc on it (you can set up SSH but it's difficult on windows)
if your PC is running linux: you need OpenSSH Server or if you want to use VNC it is probably already installed (all you need to do is enable it).

so now we will create a fairly crude script to not only wake but access the PC

open editor
Code:
pygtkeditor /home/user/MyScripts/accessremotepc.sh
copy paste this into it:
Code:
#!/bin/sh
# Waits for machine to boot and keeps trying to access it (retries only with ssh)
# by Cue from maemo talk

initialwait=20
retrywait=10

waitmessage()
{
count=$1
while [ $count -ge 0 ]
do
    sleep 1
    clear
    echo $2 $count $3
    count=`expr $count - 1`
done
}

# is there the right number of arguments
if [ $# -ne 1 ] && [ $# -ne 3 ]
then
   echo "Usage $0 [MAC_ADDRESS] [USER] [HOST]"
   exit 2              
fi

if [ $# -eq 3 ]
then
    ssh $2@$3
    error=$?
else
    error=1
fi

if [ $error -ne 0 ]; then
    echo "machine probably not on. sending magic packet."            
    #wake machine with previously written shell script 
    /home/user/MyScripts/wakeonlan.py $1
    #use this instead if you use the indirect wake on lan script
    #/home/user/MyScripts/wakeonlan.sh $1
    error=$?
    if [ $error -ne 0 ]
    then
        echo "wake on lan script  was not found encountered an error"        
    else
        
        #Wait for "initialwait" seconds for machine to boot
        echo "Magic Packet Sent"
        waitmessage $initialwait 'Waiting' 'seconds for remote machine to boot'
        
        if [ $# -eq 1 ]; then
            vncviewer
        else
        
            while true
            do    
                    
                ssh $2@$3
                error=$?
                if [ $error -ne 0 ]; then
                    waitmessage $retrywait 'Retrying in' 'seconds, press Ctrl+C to exit'
                    echo "Trying to connect"
                else
                    break
                fi
            done
        fi
    fi
fi
into the editor and save. don't forget:
Code:
chmod a+x /home/user/MyScripts/remoteaccesspc.sh

Now this command in the x terminal will wake up (if not already awake), then access the pc with either vnc or ssh

for vnc
Code:
/home/user/MyScripts/accessremotepc.sh "YOUR PC MAC ADDRESS"
for ssh
Code:
/home/user/MyScripts/accessremotepc.sh "YOUR_PC_MAC_ADDRESS" "USERNAME_TO_LOG_IN_WITH" "HOST"
example: ssh to a pc with a mac address 00:1A:2B:3C:4D:5F and an ip 192.168.1.2 as root user
Code:
/home/user/MyScripts/accessremotepc.sh 00:1A:2B:3C:4D:5F root 192.168.1.2
5) Creating a shortcut on the desktop
Everything we have done so far has been using the command line to write scripts so we can do even more in the command line. we will now create a nice shortcut on the desktop or apps menu to access a certain PC with one click (and probably a password entry or two).

you will need rootsh installed (this is now becoming more dangerous)
also install leafpad if you dont like the vi editor

Code:
sudo gainroot
Code:
leafpad /usr/share/applications/hildon/MyPC.desktop
paste the following into the text editor, make sure you replace "YOUR..." with your PC details and save

Code:
[Desktop Entry]
Encoding=UTF-8
Type=Application
Name=MyPC
Comment=accessmypc
Exec=osso-xterm '/home/user/MyScripts/accessremotepc.sh "YOUR_PC_MAC_ADDRESS" "USERNAME_TO_LOG_IN_WITH" "YOUR_HOST"'
Icon=general_share
that's it. you now have an icon you can add to your desktop or access from the applications menu. if that's not dangerous enough for you, there's more, if you laugh at the face of danger you can even create a "My PCs" directory in the menu with shortcuts to many PCs (read this thread http://talk.maemo.org/showthread.php?t=37874&page=3)

Lets just hope a WOL app becomes available in extras in the future.
Thank you to everyone for making the forums what it is and I welcome any scripting suggestions from the gurus.

Last edited by Cue; 2010-04-15 at 02:02.
 

The Following 10 Users Say Thank You to Cue For This Useful Post:
2disbetter's Avatar
Posts: 365 | Thanked: 98 times | Joined on Nov 2009
#2
thanks for this. Having a small problem with it though. Everytime I try to run it with my MAC, i get a

IndexError: list index out or range

Any idea what the problem is?

Edit: I too hope a WOL app comes to the N900, specially one that will allow you to send magic packets out over the internet as well.

2d
 
mason's Avatar
Posts: 93 | Thanked: 36 times | Joined on Nov 2009 @ Germany / Mainz
#3
you have to specify your mac adress like this : "FF:FF:FF:FF:FF:FF"
mayebe thats your problem ... also make sure you call the bash script and not the python script in the hildon shortcut. So if you use "method 1" make a second script to call the python script...
 

The Following User Says Thank You to mason For This Useful Post:
Posts: 840 | Thanked: 823 times | Joined on Nov 2009
#4
Originally Posted by 2disbetter View Post
thanks for this. Having a small problem with it though. Everytime I try to run it with my MAC, i get a

IndexError: list index out or range

Any idea what the problem is?

Edit: I too hope a WOL app comes to the N900, specially one that will allow you to send magic packets out over the internet as well.

2d
I think mason is right. it's probably how you entered the mac address. if not, does it display a line number before the error message? that could help.

if your trying to Wake over the internet using the first method only with a custom port, you should try changing this line
Code:
s.sendto(msg, ('<broadcast>', 9))
to something like

Code:
s.sendto(msg, ('YOUR_DEST', YOUR_PORT))
and have your router forward anything on that port to your LAN broadcast address. which is usually something like 192.168.1.255; or if you have only one machine to wake just forward it to its lan ip.
I've not tried WOL over the internet with the first script but you could give it a go.
 

The Following User Says Thank You to Cue For This Useful Post:
Posts: 70 | Thanked: 6 times | Joined on Oct 2009
#5
Code:
[Desktop Entry]
Encoding=UTF-8
Type=Application
Name=WOL
Comment=WOL
Exec=osso-xterm '/home/user/Myscripts/wakeonlan.sh "XX:XX:XX:XX:XX"'
Icon=wol

is this right for wol shortcut
 
Posts: 840 | Thanked: 823 times | Joined on Nov 2009
#6
Originally Posted by no mercy View Post
Code:
[Desktop Entry]
Encoding=UTF-8
Type=Application
Name=WOL
Comment=WOL
Exec=osso-xterm '/home/user/Myscripts/wakeonlan.sh "XX:XX:XX:XX:XX"'
Icon=wol

is this right for wol shortcut
seems ok but be careful with the path and script name, they are case sensitive. Also, unless you created a wol icon the icon name you entered will not work.

I noticed that I wasn't consistent with the upper case S in "MyScripts" I've edited it to correct this. if you used an upper case or lower case S make sure you use the same throughout.

Last edited by Cue; 2010-01-23 at 20:38.
 
2disbetter's Avatar
Posts: 365 | Thanked: 98 times | Joined on Nov 2009
#7
Originally Posted by Cue View Post
I think mason is right. it's probably how you entered the mac address. if not, does it display a line number before the error message? that could help.

if your trying to Wake over the internet using the first method only with a custom port, you should try changing this line
Code:
s.sendto(msg, ('<broadcast>', 9))
to something like

Code:
s.sendto(msg, ('YOUR_DEST', YOUR_PORT))
and have your router forward anything on that port to your LAN broadcast address. which is usually something like 192.168.1.255; or if you have only one machine to wake just forward it to its lan ip.
I've not tried WOL over the internet with the first script but you could give it a go.
Thanks Cue and mason. Mason was right about the MAC entry. I got it to work on a LAN after that. I edited the scripted and saved as it wakeonlani.py for internet attempts.

Now i just need to get my routers all forwarding correctly. For some reason they don't seem to like UDP 9.

2d
 
Posts: 70 | Thanked: 6 times | Joined on Oct 2009
#8
is there any chance that i can make a shortcut to a list of wol clients?

press wol shortcut will bring you to a list, than you can chosse which wol client ( script) will be activated
 
Posts: 840 | Thanked: 823 times | Joined on Nov 2009
#9
Originally Posted by no mercy View Post
is there any chance that i can make a shortcut to a list of wol clients?

press wol shortcut will bring you to a list, than you can chosse which wol client ( script) will be activated
one way is the method given in the end of the tutorial and that is to edit the menu so that you have a directory with many shortcuts to different PCs. This however is rather dangerous since making a mistake can send your N900 into an endless loop of restarts.

There is another way which is safer and that is with another script and a type of config file.

create a file called pclist.cfg in the MyScripts directory

Code:
pygtkeditor ./MyScripts/pclist.cfg
with a structure like this
Code:
[HomePC]
mac="FF:FF:FF:FF:FF:FF"
ip="192.168.1.4"
user="uername"
type="vnc"

[OfficePC]
mac="FF:FF:FF:FF:FF:FF"
ip="192.168.1.5"
type="ssh"

[UniPC]
mac="EE:EE:FF:FF:FF:FF"
ip="192.168.1.6"
user="me"
type="ssh"
so you have your pc name in brackets followed by the information for that PC, add as menu entries as you see fit.

now create the following script named pclistwol.sh

Code:
pygtkeditor ./MyScripts/pclistwol.sh
which reads this file and performs whatever action you like at the bottom with the information in that file paste this and save
Code:
#!/bin/sh
# Function: get_config_list config_file
# Purpose : Print the list of configs from config file
get_config_list()
{
   config_file=$1
   awk -F '[][]' '
      NF==3 && $0 ~ /^\[.*\]/ { print $2 }
   ' ${config_file}
}

# Function : set_config_vars config_file config [var_prefix]
# Purpose  : Set variables (optionaly prefixed by var_prefix) from config in config file
set_config_vars()
{
   config_file=$1 #file name
   config=$2 #set name
   var_prefix=$3 #prefix variable with this
   config_vars=$( 
        awk -F= -v Config="${config}" -v Prefix="${var_prefix}" '
        BEGIN { 
           Config = toupper(Config);
           patternConfig = "\\[" Config "]";
        }
        toupper($0)  ~ patternConfig,(/\[/ && toupper($0) !~ patternConfig)  { 
           if (/\[/ || NF <2) next;
           sub(/^[[:space:]]*/, "");
           sub(/[[:space:]]*=[[:space:]]/, "=");
           print Prefix $0;
        } ' ${config_file} )

   eval "${config_vars}"
}

# Set variables for selected pc
file="/home/user/MyScripts/pclist.cfg"
if [ $# -ne 1 ]
then
 echo "List of PCs:"
 for cfg in $(get_config_list ${file})
 do
   echo "--- [${cfg}] ---"
   unset $(set | awk -F= '/^cfg_/  { print $1 }') cfg_
 done
 echo "which do you wish to wake"
 read cfg
   set_config_vars ${file} ${cfg} cfg_
else
   set_config_vars ${file} $1 cfg_
fi

#Use config file to perform action eg wol from PC list.
/home/user/MyScripts/wakeonlan.py $cfg_mac
this script needs cleaning but I think it works. at the bottomline notice I've called the wol python script but you can use whatever script you want

make sure you do

chmod a+x ./MyScripts/pclist.cfg
chmod a+x ./MyScripts/pclistwol.sh

so now

./MyScripts/pclistwol.sh OfficePC

will wake the office PC, or

./MyScripts/pclistwol.sh

will list the pcs and ask which I want to wake. you can create a shortcut for the above.

Last edited by Cue; 2010-01-24 at 03:50.
 

The Following User Says Thank You to Cue For This Useful Post:
2disbetter's Avatar
Posts: 365 | Thanked: 98 times | Joined on Nov 2009
#10
Has anyone else attempted to use this script over the internet for WoL?

Trying to see if the script broadcasts the packet correctly over the nets.

2d
 
Reply


 
Forum Jump


All times are GMT. The time now is 18:22.