ActiveState Code

Recipe 358449: Wake On Lan


Switches on remote computers using WOL.

Python
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#!/usr/bin/env python
# wol.py

import socket
import struct

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, ('<broadcast>', 7))
    

if __name__ == '__main__':
    # Use macaddresses with any seperators.
    wake_on_lan('0F:0F:DF:0F:BF:EF')
    wake_on_lan('0F-0F-DF-0F-BF-EF')
    # or without any seperators.
    wake_on_lan('0F0FDF0FBFEF')

Discussion

Would be helpful if you were automating installs overnight and need switched off computers to be turned on automatically.

Comments

  1. 1. At 7:44 a.m. on 15 dec 2004, Eric Koome said:

    Great stuff. The Mac address can also be in the 000D9DD95A4B format. Is there anything like shutdown on LAN (SOL)?

  2. 2. At 6:07 a.m. on 23 dec 2004, Fadly Tabrani (the author) said:

    Mac Address Format. Thanks for pointing that out Eric. I've included it in the example.

    For "SOL" ;) though, afraid that can't be done. There are ways however to shutdown remote computers. On Windows (NT-based only), it can be done using RPC/WMI calls. I'll post a recipe on that soon, look out for it.

  3. 3. At 3:10 p.m. on 28 dec 2004, Jason Whitlark said:
  4. 4. At 7:37 p.m. on 1 apr 2007, s g said:

    problem running script. It is not clear to me why I get this error on my MacBook Pro:

    shaneg@shane-gs-computer:~\ 21:25:57$ sudo python /tmp/wol.py

    Traceback (most recent call last):

    File "/tmp/wol.py", line 34, in

    wake_on_lan('0F:0F:DF:0F:BF:EF')
    

    File "/tmp/wol.py", line 30, in wake_on_lan

    sock.sendto(send_data, ('', 7))
    

    socket.error: (49, "Can't assign requested address")

    shaneg@shane-gs-computer:~\ 21:26:02$

    I assume '<broadcast>>' should be replaced with the broadcast address for your LAN--which is usually the IP address after the last usable IP address in your network range. In a 192.168.1.0/24 network, the broadcast address would be 192.168.1.255 (unless configured otherwise).

    Anwyay, that's my assumption, so I now changed one line to look like this: sock.sendto(send_data, ('192.168.1.255', 7)) The script does not give me errors when I run it that way. I haven't yet tested it to see if it actually does WOL, as advertised. (If I am correct, then passing in the broadcast_addr would be a nice change of the function.)

  5. 5. At 11:49 a.m. on 30 oct 2008, Benjamin Sergeant said:

    shaneg, you should find your Mac Adress and replace the call:

    wake_on_lan('0F:0F:DF:0F:BF:EF')

    with your own Mac Adress. On a mac go in Settings / Network / Advanced / Ethernet.

  6. 6. At 9:11 p.m. on 13 mar 2009, Rodrigo da Silva Guerra said:

    Thanks, this helped a lot.

Sign in to comment