Wake on lan works

I previously tried to get wake-on-lan to work and had no luck. I was never sure if my code was wrong, or if a firewall was filtering the command of if my motherboard just doesn't support it (probably the latter).
Today I got a new computer and the old wake-on-lan.py worked!

So here's the program that works for me.


#!/usr/bin/env python

import socket
import struct
import sys

def wake_on_lan(macaddress):
""" Switches on remote computers using WOL. """

# Check macaddress format and try to compensate.
if len(macaddress) == 12:
pass
elif len(macaddress) == 12 + 5:
sep = macaddress[2]
macaddress = macaddress.replace(sep, '')
else:
raise ValueError('Incorrect MAC address format')

# Pad the synchronization stream.
data = ''.join(['FFFFFFFFFFFF', macaddress * 20])
send_data = ''

# Split up the hex values and pack.
for i in range(0, len(data), 2):
send_data = ''.join([send_data,
struct.pack('B', int(data[i: i + 2], 16))])

# Broadcast it to the LAN.
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
sock.sendto(send_data, ('255.255.255.255', 7))
sock.close()

if __name__ == '__main__':
machines = {
'hera' : '01-23-45-67-89-AB',
'zeus' : 'CD:EF:01:12:45:67',
}
machine = 'scott'
if len(sys.argv) > 1:
machine = sys.argv[1].strip()
wake_on_lan(machines[machine])


In the BIOS settings I turned on both "PME Event Wakeup" and "Power On Ring" (neither of which is written "wake on LAN").

I'm quite happy with the computer, it has a very silent power supply and I also got a 22" Samsung LCD to go with it.
No I'll have to go and setup my old computer for Victor.

Comments

Popular posts from this blog

Shortest Sudoku solver in Python

Dot to Png

Seven Segment Display in Inkscape