Created
August 17, 2014 05:09
-
-
Save emptypage/6bfb3217c37334482317 to your computer and use it in GitHub Desktop.
Send the magic packet of Wake on LAN
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
#!/usr/bin/env python | |
import binascii | |
import socket | |
PORT_DEFAULT = 9 | |
IPADDR_DEFAULT = '<broadcast>' | |
_USAGE = """Usage: | |
python wol.py [options] <MAC address>... | |
Options: | |
-p, --port <port> | |
Default port is 9 | |
-a, --address <IP address> | |
Default IP address is '<broadcast>' | |
-h, --help | |
Show this help. | |
""" | |
def sendmagickpacket(macs, ipaddr=IPADDR_DEFAULT, port=PORT_DEFAULT): | |
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) | |
s.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1) | |
for mac in macs: | |
for sep in ':-': | |
if sep in mac: | |
mac = ''.join([x.rjust(2, '0') for x in mac.split(sep)]) | |
break | |
mac = mac.rjust(12, '0') | |
p = '\xff' * 6 + binascii.unhexlify(mac) * 16 | |
s.sendto(p, (ipaddr, port)) | |
s.close() | |
def main(): | |
import getopt | |
import sys | |
try: | |
opt, args = getopt.getopt(sys.argv[1:], 'hp:a:', | |
['help', 'port=', 'address=']) | |
except getopt.GetoptError: | |
print _USAGE | |
sys.exit(2) | |
if not args: | |
print _USAGE | |
sys.exit(2) | |
port = PORT_DEFAULT | |
ipaddr = IPADDR_DEFAULT | |
for k, v in opt: | |
if k in ('-h', '--help'): | |
print _USAGE | |
sys.exit() | |
if k in ('-p', '--port'): | |
port = int(v) | |
if k in ('-a', '--address'): | |
ipaddr = v | |
sendmagickpacket(args, ipaddr, port) | |
if __name__ == '__main__': | |
main() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment