Tested on QNAP QTS 5.0.1 with Python 2.7.13.
#!/usr/local/bin/python
import re, sys, struct, socket
def wake(mac_string):
# Convert mac address string
mac_segments = mac_string.split(':')
mac_int = struct.pack('BBBBBB',
int(mac_segments[0], 16),
int(mac_segments[1], 16),
int(mac_segments[2], 16),
int(mac_segments[3], 16),
int(mac_segments[4], 16),
int(mac_segments[5], 16))
# Build magic packet
magic_packet = '\xff' * 6 + mac_int * 16
# Send magic packet to broadcast address on UDP port 9
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
sock.sendto(magic_packet, ("<broadcast>", 9))
sock.close()
# Compile mac address pattern
mac_pattern = re.compile("^[0-9a-f]{2}(:[0-9a-f]{2}){5}$", re.IGNORECASE)
# Handle input and output
if len(sys.argv) != 2:
print "Usage: wol.py <mac_address>"
print "Example: wol.py 00:11:22:33:44:55"
sys.exit(1)
elif mac_pattern.match(sys.argv[1]):
wake(sys.argv[1])
print "wol.py: Magic packet was sent to " + sys.argv[1] + "."
else:
print "Invalid mac address format: " + sys.argv[1]
print "Use format 00:11:22:33:44:55"
sys.exit(2)