2014-05-31, 00:02
Currently I am using a custom addon I made, that ssh's into my home server and issues the shutdown command. This works perfectly. I also use Advanced WOL to turn the server back on. I am attempting to create an addon that will perform the functions of both of those.
Later, I will attempt to make an add-on with a settings page for the variables so anyone could install and use it, but for the moment I am just trying to get the basics down. This is what I have at the moment...
This is based on a couple of scripts I found, and a VERY rudimentary knowledge of python. So can anyone tell me where I am going wrong?
Later, I will attempt to make an add-on with a settings page for the variables so anyone could install and use it, but for the moment I am just trying to get the basics down. This is what I have at the moment...
Code:
import xbmc
import xbmcaddon
import os
import struct, socket
import 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()
temp = "ping 10.0.1.200 -c 3"
ping_raw = os.popen(temp)
ping_output = ping_raw.read()
if ping_output.find("bytes") != -1:
os.system('ssh user@ServerIP "sudo shutdown -h now"')
else:
WakeOnLan('XX:XX:XX:XX:XX:XX')
This is based on a couple of scripts I found, and a VERY rudimentary knowledge of python. So can anyone tell me where I am going wrong?