Update wol.py
Update wol.py

removed whitespaces

file:a/wol.py -> file:b/wol.py
#!/usr/bin/env python3 #!/usr/bin/env python3
# #
# 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
import re import re
   
   
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
#Get the mac numbers #Get the mac numbers
numbers = re.findall('([a-fA-F0-9]{2})',macaddress) numbers = re.findall('([a-fA-F0-9]{2})',macaddress)
#We must have 6 result, or the mac is invalid #We must have 6 result, or the mac is invalid
if(len(numbers) == 6): if(len(numbers) == 6):
#If the result is correct, join it into a string #If the result is correct, join it into a string
macaddress= ''.join(numbers) macaddress= ''.join(numbers)
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 = b'' send_data = b''
   
# 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 = b''.join([send_data, send_data = b''.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()