Made script executable
Made script executable

file:a/wol.py -> file:b/wol.py
#!/usr/bin/env python #!/usr/bin/env python
# #
# Based on wol.py from http://code.activestate.com/recipes/358449-wake-on-lan/ # Based on wol.py from http://code.activestate.com/recipes/358449-wake-on-lan/
# Amended to use configuration file and hostnames # Amended to use configuration file and hostnames
# #
# Copyright (C) Fadly Tabrani, B Tasker # Copyright (C) Fadly Tabrani, B Tasker
# #
# Released under the PSF License See http://docs.python.org/2/license.html # Released under the PSF License See http://docs.python.org/2/license.html
# #
# #
   
   
import socket import socket
import struct import struct
import os import os
import sys import sys
import ConfigParser import ConfigParser
   
   
myconfig = {} myconfig = {}
   
   
def wake_on_lan(host): def wake_on_lan(host):
""" Switches on remote computers using WOL. """ """ Switches on remote computers using WOL. """
global myconfig global myconfig
   
try: try:
macaddress = myconfig[host]['mac'] macaddress = myconfig[host]['mac']
   
except: except:
return False return False
   
# Check macaddress format and try to compensate. # Check macaddress format and try to compensate.
if len(macaddress) == 12: if len(macaddress) == 12:
pass pass
elif len(macaddress) == 12 + 5: elif len(macaddress) == 12 + 5:
sep = macaddress[2] sep = macaddress[2]
macaddress = macaddress.replace(sep, '') macaddress = macaddress.replace(sep, '')
else: else:
raise ValueError('Incorrect MAC address format') raise ValueError('Incorrect MAC address format')
# Pad the synchronization stream. # Pad the synchronization stream.
data = ''.join(['FFFFFFFFFFFF', macaddress * 20]) data = ''.join(['FFFFFFFFFFFF', macaddress * 20])
send_data = '' send_data = ''
   
# Split up the hex values and pack. # Split up the hex values and pack.
for i in range(0, len(data), 2): for i in range(0, len(data), 2):
send_data = ''.join([send_data, send_data = ''.join([send_data,
struct.pack('B', int(data[i: i + 2], 16))]) struct.pack('B', int(data[i: i + 2], 16))])
   
# Broadcast it to the LAN. # Broadcast it to the LAN.
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1) sock.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
sock.sendto(send_data, (myconfig['General']['broadcast'], 7)) sock.sendto(send_data, (myconfig['General']['broadcast'], 7))
return True return True
   
   
def loadConfig(): def loadConfig():
""" Read in the Configuration file to get CDN specific settings """ Read in the Configuration file to get CDN specific settings
   
""" """
global mydir global mydir
global myconfig global myconfig
Config = ConfigParser.ConfigParser() Config = ConfigParser.ConfigParser()
Config.read(mydir+"/.wol_config.ini") Config.read(mydir+"/.wol_config.ini")
sections = Config.sections() sections = Config.sections()
dict1 = {} dict1 = {}
for section in sections: for section in sections:
options = Config.options(section) options = Config.options(section)
   
sectkey = section sectkey = section
myconfig[sectkey] = {} myconfig[sectkey] = {}
   
   
for option in options: for option in options:
myconfig[sectkey][option] = Config.get(section,option) myconfig[sectkey][option] = Config.get(section,option)
   
   
return myconfig # Useful for testing return myconfig # Useful for testing
   
def usage(): def usage():
print 'Usage: wol.py [hostname]' print 'Usage: wol.py [hostname]'
   
   
   
if __name__ == '__main__': if __name__ == '__main__':
mydir = os.path.dirname(os.path.abspath(__file__)) mydir = os.path.dirname(os.path.abspath(__file__))
conf = loadConfig() conf = loadConfig()
   
try: try:
# Use macaddresses with any seperators. # Use macaddresses with any seperators.
   
if sys.argv[1] == 'list': if sys.argv[1] == 'list':
print 'Configured Hosts:' print 'Configured Hosts:'
for i in conf: for i in conf:
if i != 'General': if i != 'General':
print '\t',i print '\t',i
print '\n' print '\n'
else: else:
   
if not wake_on_lan(sys.argv[1]): if not wake_on_lan(sys.argv[1]):
print 'Invalid Hostname specified' print 'Invalid Hostname specified'
else: else:
print 'Magic packet should be winging its way' print 'Magic packet should be winging its way'
except: except:
usage() usage()