Wake on Lan script?

Requests, suggestions, support, and everything else python / plugin related.
Post Reply
trogggy
Posts: 106
Joined: Fri Jul 06, 2012 1:24 pm
Has thanked: 4 times
Been thanked: 12 times

Wake on Lan script?

Post by trogggy »

Anyone?

I tried using the advanced wol plug-in from the xbmc.org repo...
Tried an ancient xbox script I found at xbmc.org...
I have wol enabled on my server, I can wake it from my pi or pc - now I want the xbox to be able to give it a kick!
Any help gratefully received.
User avatar
Xphazer
Posts: 524
Joined: Wed Jul 04, 2012 4:39 am
Location: Montréal
Has thanked: 240 times
Been thanked: 106 times

Re: Wake on Lan script?

Post by Xphazer »

That's what I was using back in the days when my server was not running 24/7. Use autoexec.py to run it at boot or map it to a remote button.

Code: Select all

# 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

import struct, socket

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()

# Example use
WakeOnLan('00:00:00:00:00:00')
This next script is to be run on a Linux server, it check if XBMC is running on your selected client and shut down /put to sleep the server upon inactivity.

Code: Select all

#!/usr/bin/python

###########################
# HTPC suspend script     #
# author: Hans van Schoot #
###########################
# TODO list: build in a check for ftp server activity

# The purpose of this script is to suspend the machine if it's idle.
# the script checks:
# - if a lockfile is present (this way the script can be bypassed when needed)
# - if XBMC is running, and if it's playing
# - if there is keyboard or mouse activity
# - if transmission download speed is too low to be keeping the system awake
# - if there are samba shares in use (READ ON FOR THIS!)

# To function properly this script needs a couple things:
# - from apt-get: xprintidle
# - from apt-get: transmissioncli
# - the gnome-power-manager script (installed in /usr/bin) from AgenT: http://ubuntuforums.org/showpost.php?p=8309702&postcount=16
# - xbmc web server enabled without a password (can be found in xmbc settings under network) 
#     (if you don't need xbmc, you can comment out the xbmc section or put the xbmcDelay on 0)
# - to be able to use "sudo smbstatus" (so rootaccess, or if you run the script on userlevel this can be fixed by adding
#     "your_username ALL=(ALL) NOPASSWD: /usr/bin/smbstatus" visudo (run "sudo visudo" in terminal))

# if you're running a minimal ubuntu install (without a x session, like gnome or kde) you can't use/don't need
# the xprintidle stuff. you can remove this from the script, or just don't install xprintidle and put the xDelay on 0


#############################
# Settings and delay values #
#############################
# the system suspends only if all the different items are idle for a longer time than specified for it
# the values are in minutes, unless you change the sleep command at the start of the loop
xbmcDelay = 10
sambafileDelay = 10
xDelay = 10
transmissionDelay = 0
# this is the path to the lockfile you can use.
# to lock the system, just use the touchcommand: "touch /home/media/.suspendlockfile" (or create a starter to it)
# to unlock the system, just use rmcommand "rm /home/media/.suspendlockfile" (or create a starter to it)
lockfilePath = "/home/media/.suspendlockfile.txt"
# the path to the xmbc webserver, edit this if you use another port for instance
xbmcAdress = "http://192.168.1.105:80/xbmcCmds/xbmcHttp?command=getcurrentlyplaying"
# logindata for the transmission server (this is the same data you need to enter when contacting the demon through the web interface
transmissionlogin = "enter your login here"
transmissionpass = "enter your password here"
# command to contact the transmission server
transmissionAdress = "transmission-remote -n %s:%s " %(transmissionlogin,transmissionpass)
# minimal download speed required to keep the system awake (in kb/s)
transmissionSpeed = 10.0


##### SAMBACHECK SETTINGS #######
# the script checks the output of sudo smbstatus to see if there are locked files 
# (usualy someone downloading or playing media from the system)
# if no locked files are found, it checks if there are folders in use that should keep the system awake
# 
# smbimportantlist is a list of strings the sambashare should check if they are in use (for instance a documents folder)
# to find out what to put here: 
# 1. connect to the share with another computer
# 2. use "sudo smbstatus" in console
# 3. check for the name of the share, it's printed out under "Service"
#
# makeup of the list is: [ 'item1' , 'item2' , 'item3' ]
smbimportantlist = [
'photos' ,
'IPC$' ,
'warehouse' ,
'musique' ,
'videos' ,
'inviteftp' ,
'networkdvd' ,
]

# this list checks for lockfiles in specified home directories, makeup is the same as the smblist, but with pathnames to possible lockfiles
# add/change (or remove) as many lockfile locations as you like
lockfilelist = [
'/home/user1/.suspendlockfile',
'/home/user2/.suspendlockfile',
'/home/user3/.suspendlockfile'
]

# change this to False if you want the script to run silent
debugmode = True




### the script starts here
from os import *
from urllib2 import *
from time import sleep

xIdletime = 0
xbmcIdletime = 0
sambaIdletime = 0
transmissionIdletime = 0
Lockfile = 0
keeponrunnin = True

# this is the loop that keeps the script running. the sleep command makes it wait one minute
# if you want to increase/decrease the frequencies of the checks, change the sleeptimer.
# keep in mind that this wil change the Delay times for xbmc and samba
while keeponrunnin:
    print "\n !!! Niet dichtdoen!!!\n Dit schermpje moet de pc in slaapstand zetten!\n"
    sleep(60)
# this checks when the mouse and keyboard were touched, and converts the time to minutes
    try: xIdletime = int(popen('xprintidle').read())/60000
    except IOError, e:
        if debugmode:
            print "xprintidle is not installed? use sudo apt-get install xprintidle"
        

# check XBMC for activity:
    xbmcRequest = Request(xbmcAdress)
    try: handle = urlopen(xbmcRequest)
    except IOError, e:
        if debugmode:
            print "XBMC draait niet"
        xbmcIdletime += 1
    except httplib.HTTPException:
        if debugmode:
            print "XBMC pagina httpexception error, better luck next round..."
    else:
        page = handle.read()
        if page.find('Filename:[Nothing Playing]') >= 0:
            if debugmode:
                print "Nothing Playing\n"
            xbmcIdletime += 1 
        elif page.find('PlayStatus:Playing') >= 0:
            if debugmode:
                print "XBMC is playing"
            xbmcIdletime = 0
        elif page.find('PlayStatus:Paused') >= 0:
            if debugmode:
                print "XBMC is paused"
            xbmcIdletime += 1


# counting the number of lockfiles
    Lockfile = 0
    for i in lockfilelist:
        if path.exists(i):
            Lockfile += 1

#######################################
# next section is the samba checkpart #
#######################################
    try: sambainfo = popen('sudo smbstatus').read()
    except IOError, e:
        if debugmode:
            print "No Sambaserver found, or no sudorights for smbstatus"
        sambaIdletime += 1 
    else:
        # first we check for file-locks
        if sambainfo.find('Locked files:\n') >= 0:
            sambaIdletime = 0
            if debugmode:
                print "a locked samba file has been found"
        # if no locked files, strip the info and look for keywords, if found reset idletime
        else:
            sambaIdletime += 1
            sambasplit = sambainfo.split('\n')[4:-4]
            sambasplit.reverse()
            for i in sambasplit:
                if i == '-------------------------------------------------------':
                    break
                for j in smbimportantlist:
                    # check to filter out crashes on empty lines
                    if len(i.split()) >= 1:
                        if i.split()[0].find(j) >= 0:
                            sambaIdletime = 0
                            if debugmode:
                                print "an important samba share is in use"



# this is the final check to see if the system can suspend. 
# uncomment the print statements and run in terminal if you want to debug/test the script
    if xbmcIdletime >= xbmcDelay and xIdletime >= xDelay and sambaIdletime >= sambafileDelay and transmissionIdletime >= transmissionDelay and Lockfile == 0:
        if debugmode:
            print "suspend allowed"
        system('sudo pm-hibernate')
        xbmcIdletime = 0
        sambaIdletime = 0
        transmissionIdletime = 0
    else:
        if debugmode:
            print "system is active, not suspending"
            print "xsystem is idle for ", xIdletime, " minutes"
            print "xbmc is idle for ", xbmcIdletime, " minutes"
            print "samba is idle for ", sambaIdletime, " minutes"
            print "transmission is idle for ", transmissionIdletime, " minutes"
            if Lockfile == 0:
                print "er is geen lockfile gevonden"
            else:
                print "er is/zijn %i lockfile gevonden" %(Lockfile)
User avatar
Xphazer
Posts: 524
Joined: Wed Jul 04, 2012 4:39 am
Location: Montréal
Has thanked: 240 times
Been thanked: 106 times

Re: Wake on Lan script?

Post by Xphazer »

Oupps missed the part where you saying that you tried it. lol
Can you WOL your server/PC from another PC ?
A debug log would confirm if the script is getting executed correctly.
trogggy
Posts: 106
Joined: Fri Jul 06, 2012 1:24 pm
Has thanked: 4 times
Been thanked: 12 times

Re: Wake on Lan script?

Post by trogggy »

Yep, starts no problem from pc / xbmc.
Log trying script and add-on:
http://www.xbmclogs.com/show.php?id=44057
User avatar
BuZz
Site Admin
Posts: 1891
Joined: Wed Jul 04, 2012 12:50 am
Location: UK
Has thanked: 66 times
Been thanked: 423 times
Contact:

Re: Wake on Lan script?

Post by BuZz »

Please open a ticket against 3.4dev on the bugtracker, with debug log, and then run the 3.3 release instead of the nightly which is not meant for everyday use :)
trogggy
Posts: 106
Joined: Fri Jul 06, 2012 1:24 pm
Has thanked: 4 times
Been thanked: 12 times

Re: Wake on Lan script?

Post by trogggy »

Arse! (me, not you).

I tried both on 3.3, forgot I'd switched to a nightly (to try them again!)
Log of script failing on 3.3:
http://www.xbmclogs.com/show.php?id=44095

Just to be clear - and rule out me having done something stupid - I just stick the MAC address of the server at the bottom of the script in place of the '00:00etc' and stick the script in xbmc/scripts...?
User avatar
BuZz
Site Admin
Posts: 1891
Joined: Wed Jul 04, 2012 12:50 am
Location: UK
Has thanked: 66 times
Been thanked: 423 times
Contact:

Re: Wake on Lan script?

Post by BuZz »

is it supposed to have the broadcast address in or should <broadcast> work ?

check this for details though

http://www.nyayapati.com/srao/2010/03/w ... sing-xbmc/

I guess you could try by putting the broadcast address in ?

note the quoting actually for the broadcast line - you might need that ?
trogggy
Posts: 106
Joined: Fri Jul 06, 2012 1:24 pm
Has thanked: 4 times
Been thanked: 12 times

Re: Wake on Lan script?

Post by trogggy »

Woohoo!
All good.

Downloaded xbmc.script.wol from Sourceforge courtesy of aturlov:
https://sourceforge.net/projects/xbmc-s ... rce=navbar
Unzipped, put in 'wakey wakey' folder, FTPd to xbmc/scripts.
Context menu to input server mac address, then click and wakey wakey!

And it works with build 32050.
Thanks to Xphazer and Buzz for your inputs.
User avatar
Dan Dar3
Posts: 1176
Joined: Sun Jul 08, 2012 4:09 pm
Has thanked: 273 times
Been thanked: 257 times
Contact:

Re: Wake on Lan script?

Post by Dan Dar3 »

You can also try this (download with SVN Repo Installer). Configure computer names and MACs in plugin settings.

See readme.txt if you want to put that in autoexec.py to run at startup.

https://code.google.com/p/xbmc4xbox-add ... 20on%20LAN
trogggy
Posts: 106
Joined: Fri Jul 06, 2012 1:24 pm
Has thanked: 4 times
Been thanked: 12 times

Re: Wake on Lan script?

Post by trogggy »

Thanks Dan. I'll give that a try and post the result.


Edit: That works perfectly. Thanks for your help.
trogggy
Posts: 106
Joined: Fri Jul 06, 2012 1:24 pm
Has thanked: 4 times
Been thanked: 12 times

Re: Wake on Lan script?

Post by trogggy »

@Dan

Would it be possible to stick this in the addons4xboxinstaller?
captainsensation
Posts: 155
Joined: Sat May 18, 2013 5:12 pm
Has thanked: 21 times
Been thanked: 4 times

Re: Wake on Lan script?

Post by captainsensation »

trogggy wrote:@Dan

Would it be possible to stick this in the addons4xboxinstaller?
^^this
User avatar
GoTeamScotch
Posts: 270
Joined: Sat Apr 06, 2013 2:17 am
Has thanked: 99 times
Been thanked: 75 times

Re: Wake on Lan script?

Post by GoTeamScotch »

Thanks a lot Dan! :D
Image
Remember kids, always zero-ize your HDD key!
User avatar
Dan Dar3
Posts: 1176
Joined: Sun Jul 08, 2012 4:09 pm
Has thanked: 273 times
Been thanked: 257 times
Contact:

Re: Wake on Lan script?

Post by Dan Dar3 »

If it's because it now appears in Addons4xbox Installer that would be Kozz to thank to.
Post Reply