-
Notifications
You must be signed in to change notification settings - Fork 0
/
WakeOnLan.py
45 lines (39 loc) · 1.27 KB
/
WakeOnLan.py
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
40
41
42
43
44
45
#!/usr/bin/env python
# wol.py
#
# This module is from ActiveState Code Recipes:
# http://code.activestate.com/recipes/358449-wake-on-lan/
# and patched for Python 3 with:
# http://code.activestate.com/recipes/577609-wake-on-lan-for-python-3/
#
# Example:
# import wol
# wol.wake_on_lan('70:F3:95:15:00:B5')
#
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' )
print(macaddress)
# Pad the synchronization stream
data = b'FFFFFFFFFFFF' + (macaddress * 20 ).encode()
send_data = b''
# Split up the hex values in pack
for i in range ( 0 , len (data), 2 ):
send_data += struct.pack('B' , int (data[i: i + 2 ], 16 ))
# Broadcast it to the LAN
print(send_data)
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1 )
sock.sendto(send_data, ( '192.168.1.255' , 7 ))
if __name__ == '__main__':
wake_on_lan("00:90:A9:ED:C5:11")
print('done')