Skip to content

Instantly share code, notes, and snippets.

@rschuetzler
Last active November 13, 2024 07:30
Show Gist options
  • Save rschuetzler/8854764 to your computer and use it in GitHub Desktop.
Save rschuetzler/8854764 to your computer and use it in GitHub Desktop.
#!/usr/bin/python
# A Wake on LAN program that allows you to send magic packets over the Internet
import socket, struct
class Waker():
def makeMagicPacket(self, macAddress):
# Take the entered MAC address and format it to be sent via socket
splitMac = str.split(macAddress,':')
# Pack together the sections of the MAC address as binary hex
hexMac = struct.pack('BBBBBB', int(splitMac[0], 16),
int(splitMac[1], 16),
int(splitMac[2], 16),
int(splitMac[3], 16),
int(splitMac[4], 16),
int(splitMac[5], 16))
self.packet = '\xff' * 6 + hexMac * 16 #create the magic packet from MAC address
def sendPacket(self, packet, destIP, destPort = 7):
# Create the socket connection and send the packet
s=socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.sendto(packet,(destIP,destPort))
s.close()
def wake(self, macAddress, destIP, destPort=7):
self.makeMagicPacket(macAddress)
self.sendPacket(self.packet, destIP, destPort)
print 'Packet successfully sent to', macAddress
if __name__ == '__main__':
#This is all the information that needs to be changed to make this work for you
mac = '00:11:22:33:44:55'
ip = 'example.com' #The IP address where the packet should be sent
port = 7 #The port the packet will be sent on
wol = Waker()
wol.makeMagicPacket(mac)
wol.sendPacket(wol.packet, ip, port)
print 'Packet successfully sent to', mac
@Jonpix
Copy link

Jonpix commented May 18, 2020

Line 18: macAddress should be hexMac

@rschuetzler
Copy link
Author

Good catch

@Dirki74
Copy link

Dirki74 commented Dec 13, 2020

Thanks for the Code, but something is going wrong. :(

Traceback (most recent call last):
File "C:/Users/Dirk/Downloads/python/eigenes/dtask/wol.-test.sh.py", line 41, in
wol.makeMagicPacket(mac)
File "C:/Users/Dirk/Downloads/python/eigenes/dtask/wol.-test.sh.py", line 19, in makeMagicPacket
self.packet = '\xff' * 6 + hexMac * 16 # create the magic packet from MAC address
TypeError: can only concatenate str (not "bytes") to str

@Ancient0racle
Copy link

Ancient0racle commented Dec 14, 2020

Thanks for the Code, but something is going wrong. :(

Traceback (most recent call last):
File "C:/Users/Dirk/Downloads/python/eigenes/dtask/wol.-test.sh.py", line 41, in
wol.makeMagicPacket(mac)
File "C:/Users/Dirk/Downloads/python/eigenes/dtask/wol.-test.sh.py", line 19, in makeMagicPacket
self.packet = '\xff' * 6 + hexMac * 16 # create the magic packet from MAC address
TypeError: can only concatenate str (not "bytes") to str

If you are using Python3:
Line 18: self.packet = b'\xff' * 6 + hexMac * 16 #create the magic packet from MAC address

@Dirki74
Copy link

Dirki74 commented Dec 14, 2020

@Ancient0racle
My Hero! Thank you so much!

@Bogd-an
Copy link

Bogd-an commented Aug 23, 2024

In my case work this:

Python 3

import socket

def wol(mac_address: str):
    mac_address_bytes = bytes.fromhex(mac_address.replace(':', '').replace('-', '').lower())
    magic_packet = b'\xFF' * 6 + mac_address_bytes * 16
    sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    sock.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
    sock.sendto(magic_packet, ('255.255.255.255', 9))
    sock.close()

wol('00:11:22:33:44:55')

MicroPython

import socket

def wol(mac_address):
    mac_address = mac_address.replace(':', '').replace('-', '').lower()
    mac_address_bytes = b''.join(bytes([int(mac_address[i:i+2], 16)]) for i in range(0, 12, 2))
    magic_packet = b'\xFF' * 6 + mac_address_bytes * 16
    sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    sock.sendto(magic_packet, ('255.255.255.255', 9))
    sock.close()

wol('00:11:22:33:44:55')

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment