Shutdown Remote Server & Boot with one addon
#1
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...

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?
Reply
#2
Okay, after tinkering all afternoon I finally have a working implementation... I will try to make this better, with settings to setup the mac address and IP and username, but for now, if anyone thinks they can do anything with it, here's the code I ended up with:
Code:
import xbmc
import xbmcaddon
import os
import struct, socket
import sys
import commands

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

xbmc.executebuiltin('Notification(Checking Remove Server Status,5000)')
result = commands.getoutput("ping -c 1 ServerIP")
if result.find("100% packet loss") == -1:
    result = True
    xbmc.executebuiltin('Notification(Shutting Down Remove Server,5000)')
    os.system('ssh user@ServerIP "sudo shutdown -h now"')
else:
    result = False
    xbmc.executebuiltin('Notification(Waking Remote Server,5000)')
    WakeOnLan('MA:CA:DD:RE:SS')

Obviously you would need to fill in the proper ServerIP, Username, and MAC Address for the Server/NAS that you want to power on and off with it. Also, you need to set up the XBMC device to be able to ssh in without password, and on the server you need to edit sudoers to allow user to execute shutdown without password.

Kind of unweildy I know, but now, I can shutdown my server with the remote(without extra hardware) and boot it back up the same way.
Reply

Logout Mark Read Team Forum Stats Members Help
Shutdown Remote Server & Boot with one addon0