Wake on Lan script

From Daco.tech Technical Documentation

Simple script to send a Wake on Lan packet to a computer

I made this very simple script to learn about Wake on Lan.

The goal is to send a magic packet to a target MAC address.

If the target computer has Wake on Lan enabled in its BIOS, the computer should power on.

The script will ask the user to enter a MAC address. It will detect improper MAC formats.

# Testing WoL stuff
import re
import socket
import string
from struct import pack
def get_mac():
    while True:
            raw_mac = raw_input('Enter you MAC address: ')
            clean_mac = re.findall('[a-fA-F0-9]{2}', raw_mac)
            hex_mac = [int(i, 16) for i in clean_mac]
            if len(clean_mac) != 6:
                print 'Incorrect MAC address detected. Correct format is 01:23:45:67:89:AB'
                print 'Sending magic packet to', raw_mac
        except ValueError:
            print 'Incorrect hexadecimal value detected.'
            print 'Valid hex digits are between 0-9 and A-F'
    print hex_mac
    print clean_mac
    return hex_mac
def send_wol(hex_mac):
            bcast_mac = pack('!6B', *[0xFF] * 6)
            dest_mac = pack('!6B', *hex_mac)
            send_data = bcast_mac + dest_mac * 16
            soc = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
            soc.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST,1)
            soc.sendto(send_data, ('', 9)) 
        except socket.error:
            print 'Cannot connect to socket'
hex_mac = get_mac()