Python
28 Jun 2010
 

send wake-on-land packets

 
 
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
import struct, socket
def WakeOnLan(mac):
# Construct a six-byte hardware address
hw_addr = struct.pack('BBBBBB', *(int(b, 16) for b in mac.split(':')))
# Build the Wake-On-LAN "Magic Packet"...
msg = '\xff' * 6 + hw_addr * 16
# ...and send it to the broadcast address using UDP
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
s.sendto(msg, ('<broadcast>', 9))
s.close()
if __name__=='__main__':
from sys import argv
try:
WakeOnLan(argv[1])
except (IndexError, ValueError):
print "Bad arguments. Usage: %s ma:c:ad:dr:es:s" % argv[0]