Welcome, guest | Sign In | My Account | Store | Cart

Switches on remote computers using WOL.

Python, 39 lines
 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')

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

8 comments

Eric Koome 17 years, 1 month ago  # | flag

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

Fadly Tabrani (author) 17 years, 1 month ago  # | flag

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.

s g 14 years, 10 months ago  # | flag

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.)

Benjamin Sergeant 13 years, 3 months ago  # | flag

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.

Rodrigo da Silva Guerra 12 years, 10 months ago  # | flag

Thanks, this helped a lot.

Jesús Gómez 10 years, 10 months ago  # | flag

This is for upgrade wol.py from Python2 to Python3. Had to mount it as a recipe because in the comment the diff was distorsioned.

gg00xiv 9 years, 5 months ago  # | flag

i'have tried this code and it is working fine for me : http://megasnippets.com/source-codes/python/wake_on_lan