Last active
September 30, 2017 12:34
-
-
Save kohiro37/a039d9813e350ace8c02e547e931421c to your computer and use it in GitHub Desktop.
Send magic(WOL) packet
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Send magic(WOL) packet | |
""" | |
import socket | |
import binascii | |
import sys | |
import re | |
# Broadcast address | |
BCADDR = '255.255.255.255' | |
# Port | |
PORT = 7 | |
def macstr(macaddr): | |
""" | |
Check the validity of the MAC address | |
""" | |
if len(macaddr) == 17: | |
maclist = macaddr.split(':') | |
if len(maclist) == 6: | |
if len(list(filter(lambda x: re.match('^[0-9a-fA-F]{2}', x), maclist))) == 6: | |
return ''.join(maclist) | |
print('Invalid MAC address: ' + macaddr) | |
exit() | |
def main(): | |
params = sys.argv | |
if len(params) != 2: | |
print('Usage: python3 wol.py [MAC address]') | |
print('e.g.') | |
print('python3 wol.py AA:BB:CC:11:22:33') | |
exit() | |
mdata = 'FF' * 6 + macstr(params[1]) * 16 | |
print('Sending a magic packet to ' + params[1]) | |
# Convert into binary data represented by hexadecimal string | |
mdata = binascii.unhexlify(mdata) | |
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) | |
s.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1) | |
s.sendto(mdata, (BCADDR, PORT)) | |
s.close() | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment