Forked from mjg59/gist:ab1e9a6e60ef96ca9565ae09690a396b
Last active
June 14, 2017 05:40
-
-
Save ferstar/71179b2e90e48d2a022da5dbef5e1199 to your computer and use it in GitHub Desktop.
Revisions
-
ferstar revised this gist
Jun 14, 2017 . No changes.There are no files selected for viewing
-
ferstar revised this gist
Jun 14, 2017 . 1 changed file with 19 additions and 11 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -1,34 +1,39 @@ #!/usr/bin/python import subprocess import socket import sys def encode_packet(data): output = bytearray() for i in range(0, len(data)): tmpoutput = bytearray([0, data[i]]) output.extend(tmpoutput) return output def get_mac(address, ping=False): if ping: data = [i.decode('gbk') for i in subprocess.check_output("arp -a " + address, shell=True).split()] if len(data) != 11: print("Address Error!") sys.exit(1) if data[8] == address: return data[9] return None def generate_packet(address, mcmd, scmd): mac = get_mac(address) if mac == None: mac = get_mac(address, ping=True) macdata = mac.split("-") payload = bytearray([0x00, 0x00, 0x09, 0xaa, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, int(macdata[0], 16), int(macdata[1], 16), int(macdata[2], 16), int(macdata[3], 16), int(macdata[4], 16), int(macdata[5], 16), 0x00, 0x00, mcmd, scmd]) packet = bytearray([0x00, 0x31, 0x11]) @@ -45,24 +50,27 @@ def generate_packet(address, mcmd, scmd): return packet def on(address): packet = generate_packet(address, 1, 1) sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) sock.sendto(packet, (address, 1025)) def off(address): packet = generate_packet(address, 1, 2) sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) sock.sendto(packet, (address, 1025)) if len(sys.argv) != 3: print("Usage: %s address on|off" % sys.argv[0]) sys.exit(1) if sys.argv[2] == "on": on(sys.argv[1]) elif sys.argv[2] == "off": off(sys.argv[1]) else: print("Usage: %s address on|off" % sys.argv[0]) sys.exit(1) -
Matthew Garrett created this gist
Jun 22, 2016 .There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,68 @@ #!/usr/bin/python import os import socket import sys def encode_packet(data): output = bytearray() for i in range(0, len(data)): tmpoutput = bytearray([0, data[i]]) output.extend(tmpoutput) return output def get_mac(address, ping=False): if ping: os.system("ping >/dev/null -c 1 -W 1 " + address) with open("/proc/net/arp", "r") as f: for line in f: data = line.split() if data[0] == address: return data[3] return None def generate_packet(address, mcmd, scmd): mac = get_mac(address) if mac == None: mac = get_mac(address, ping=True) macdata = mac.split(":") payload = bytearray([0x00, 0x00, 0x09, 0xaa, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, int(macdata[0], 16), int(macdata[1], 16), int(macdata[2], 16), int(macdata[3], 16), int(macdata[4], 16), int(macdata[5], 16), 0x00, 0x00, mcmd, scmd]) packet = bytearray([0x00, 0x31, 0x11]) encodedpayload = encode_packet(payload) packet.extend(encodedpayload) checksum = 0 for i in range(1, len(packet)): checksum += packet[i] checksum = checksum & 0xff packet.append(checksum) packet.append(0xff) return packet def on(address): packet = generate_packet(address, 1, 1) sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) sock.sendto(packet, (address, 1025)) def off(address): packet = generate_packet(address, 1, 2) sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) sock.sendto(packet, (address, 1025)) if len(sys.argv) != 3: print "Usage: %s address on|off" % sys.argv[0] sys.exit(1) if sys.argv[2] == "on": on(sys.argv[1]) elif sys.argv[2] == "off": off(sys.argv[1]) else: print "Usage: %s address on|off" % sys.argv[0] sys.exit(1)