Install wakeonlan via pip
mainInstall the wakeonlan package using pip to use it as a Python module or a standalone CLI tool.
pip install wakeonlanrepository·main·Indexed 43 days ago
https://github.com/remcohaszing/pywakeonlanA Python module and CLI tool for sending Wake-on-LAN magic packets to power on computers remotely. Features include the wake() high-level API, raw packet generation via create_magic_packet(), and socket management with create_socket(). Supports SecureOn passwords, custom hosts, ports, and specific network interface routing for both IPv4 and IPv6.
Install the wakeonlan package using pip to use it as a Python module or a standalone CLI tool.
pip install wakeonlanYou can import wakeonlan to send Wake-on-LAN magic packets programmatically.
Key Requirements:
Functionality:
wake(mac_address, ...): Sends a magic packet to one or more MAC addresses.mac/password (e.g., 'ff.ff.ff.ff.ff.ff/01:23:45:67:89:ab').host (defaults to 255.255.255.255) and port (defaults to 9). Note that port forwarding is required if using an external host.interface parameter to specify a local IP address of a network adapter to route the packet through.import wakeonlan
# Wake a single computer
wakeonlan.wake('ff.ff.ff.ff.ff.ff')
# Wake a single computer with a SecureOn password
wakeonlan.wake('ff.ff.ff.ff.ff.ff/01:23:45:67:89:ab')
# Wake multiple computers
wakeonlan.wake(
'ff.ff.ff.ff.ff.ff',
'00-00-00-00-00-00',
'FFFFFFFFFFFF'
)
# Wake using a specific host and port
wakeonlan.wake('ff.ff.ff.ff.ff.ff', host='example.com', port=1337)
# Wake using a specific network interface
wakeonlan.wake('ff.ff.ff.ff.ff.ff', interface='192.168.0.2')The wakeonlan command can be used from the terminal to wake one or more computers.
Usage:
wakeonlan [options] mac_address [mac_address ...]
Arguments:
mac address: The MAC address or mac address/secureon password tuple.Options:
-h, --help: Show help message.-o HOST, --host HOST: Host name to send the magic packet to (default: 255.255.255.255).-p PORT, --port PORT: Port to send the magic packet to (default: 9).-n INTERFACE, --interface INTERFACE: IP address of the network adapter to route through (default: None).-4, --ipv4: Use IPv4.-6, --ipv6: Use IPv6.$ wakeonlan --help
usage: wakeonlan [-h] [-4] [-6] [-o HOST] [-p PORT] [-n INTERFACE] mac address [mac address ...]
Wake one or more computers using the wake on lan protocol.
positional arguments:
mac address The mac addresses or "mac address/secureon password" tuples of the computers you are trying to wake.
options:
-h, --help show this help message and exit
-o HOST, --host HOST The host name to send the magic packet to. (default: 255.255.255.255)
-p PORT, --port PORT The port of the host to send the magic packet to. (default: 9)
-n INTERFACE, --interface INTERFACE
The ip address of the network adapter to route the magic packet through. (default: None)
-4, --ipv4 To indicate ipv4 should be used. (default: False)
-6, --ipv6 To indicate ipv6 should be used. (default: False)The wake() function is the primary high-level API for sending Wake-on-LAN magic packets to one or more machines. It handles packet creation and socket management automatically.
Parameters:
*macs: One or more MAC addresses or "mac address/secureon password" tuples.host: The IP address/hostname to broadcast to (defaults to BROADCAST_IP / '255.255.255.255').port: The port to send the packet to (defaults to DEFAULT_PORT / 9).interface: The IP address of the specific network adapter to use.family: The socket.AddressFamily (e.g., socket.AF_INET for IPv4, socket.AF_INET6 for IPv6). If not specified, it auto-detects.Note: Wake-on-LAN must be enabled on the target device for this to work.
from wakeonlan import wake
import socket
# Wake a single machine
wake('00:11:22:33:44:55')
# Wake multiple machines with a SecureOn password
wake('00:11:22:33:44:55/password', 'AA:BB:CC:DD:EE:FF/secret')
# Force IPv4 usage
wake('00:11:22:33:44:55', family=socket.AF_INET)The send_magic_packet() function is deprecated. Users should migrate to using wake() instead.
Legacy Signature:
send_magic_packet(*macs, ip_address=BROADCAST_IP, port=DEFAULT_PORT, interface=None, address_family=socket.AF_UNSPEC)
The create_socket() function returns a configured socket.socket object ready for sending magic packets. It automatically handles address resolution (IPv4/IPv6) and sets the SO_BROADCAST option.
Arguments:
host: The hostname/IP to connect to.port: The target port.interface: (Optional) The IP address of the network adapter to bind to.family: The socket.AddressFamily to use. Defaults to socket.AF_UNSPEC (auto-detect).Returns:
socket.socket: A connected UDP socket suitable for broadcasting.import socket
from wakeonlan import create_socket, create_magic_packet
# Create a socket for a specific interface and IPv4
with create_socket(host='255.255.255.255', port=9, interface='192.168.1.5', family=socket.AF_INET) as sock:
packet = create_magic_packet('00:11:22:33:44:55')
sock.send(packet)Use create_magic_packet() to generate the raw bytes of a Wake-on-LAN magic packet. This is useful if you want to handle the network transmission yourself.
Arguments:
macaddress: A string representing the MAC address (e.g., '00:11:22:33:44:55') or a tuple in the format 'mac address/secureon password'.Returns:
bytes: The constructed magic packet.Errors:
ValueError if the MAC address or SecureOn password format is incorrect.from wakeonlan import create_magic_packet
# Standard MAC address
packet = create_magic_packet('00:11:22:33:44:55')
# MAC address with SecureOn password
packet_with_password = create_magic_packet('00:11:22:33:44:55/my_password')