Created
February 5, 2026 16:55
-
-
Save edthrn/c06156c7a49f59893524e215965d9322 to your computer and use it in GitHub Desktop.
A Python Script to Wake-On-Lan
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 characters
| """ | |
| Wake-on-LAN (WoL) Utility | |
| This script sends a Wake-on-LAN magic packet to a target network device, | |
| allowing it to be powered on remotely. The magic packet is constructed | |
| using the device's MAC address and sent via UDP broadcast to the specified | |
| IP address and port. | |
| Features: | |
| - Validates MAC address, IP address, and port inputs. | |
| - Supports multiple MAC address formats (e.g., 00:11:22:33:44:55, | |
| 00-11-22-33-44-55, or 001122334455). | |
| - Uses UDP broadcast to send the magic packet. | |
| - Provides command-line interface with examples and error handling. | |
| Usage: | |
| Run the script with required arguments: | |
| python wol.py --mac <MAC_ADDRESS> [--ip <BROADCAST_IP>] [--port <PORT>] | |
| Dependencies: | |
| - Python 3.x | |
| - Standard libraries: socket, argparse, sys | |
| Exit Codes: | |
| 0: Success | |
| 1: Validation or network error | |
| 130: Aborted by user (Ctrl+C) | |
| """ | |
| import socket | |
| import argparse | |
| import sys | |
| def validate_mac(mac_address): | |
| """Validate and normalize MAC address to 12 hex digits.""" | |
| mac_clean = mac_address.replace(':', '').replace('-', '').replace('.', '').replace(' ', '').lower() | |
| if len(mac_clean) != 12: | |
| raise ValueError(f"MAC address must be 12 hex digits, got {len(mac_clean)}") | |
| if not all(c in '0123456789abcdef' for c in mac_clean): | |
| raise ValueError("MAC address contains invalid characters") | |
| return mac_clean | |
| def validate_ip(ip_address): | |
| """Validate IP address format.""" | |
| try: | |
| socket.inet_aton(ip_address) | |
| except socket.error: | |
| raise ValueError(f"Invalid IP address: {ip_address}") | |
| def validate_port(port): | |
| """Validate port number.""" | |
| if not 0 < port < 65536: | |
| raise ValueError(f"Port must be between 1 and 65535, got {port}") | |
| def wake_on_lan(mac_address, ip_address, port): | |
| """Send Wake-on-LAN magic packet to target MAC address.""" | |
| mac_clean = validate_mac(mac_address) | |
| validate_ip(ip_address) | |
| validate_port(port) | |
| mac_bytes = bytes.fromhex(mac_clean) | |
| magic_packet = b'\xff' * 6 + mac_bytes * 16 | |
| try: | |
| with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as sock: | |
| sock.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1) | |
| bytes_sent = sock.sendto(magic_packet, (ip_address, port)) | |
| print(f"Sent {bytes_sent} bytes to {mac_address} via {ip_address}:{port}") | |
| return True | |
| except socket.error as e: | |
| print(f"Network error: {e}", file=sys.stderr) | |
| return False | |
| def main(): | |
| parser = argparse.ArgumentParser( | |
| description='Send Wake-on-LAN magic packet to wake a network device', | |
| formatter_class=argparse.RawDescriptionHelpFormatter, | |
| epilog=""" | |
| Examples: | |
| %(prog)s --mac 00:11:22:33:44:55 | |
| %(prog)s --mac 00-11-22-33-44-55 --ip 192.168.1.255 | |
| %(prog)s --mac 001122334455 --ip 10.0.0.255 --port 7 | |
| """ | |
| ) | |
| parser.add_argument('--mac', required=True, | |
| help='MAC address (formats: 00:11:22:33:44:55, 00-11-22-33-44-55, or 001122334455)') | |
| parser.add_argument('--ip', default='255.255.255.255', | |
| help='Broadcast IP address (default: 255.255.255.255)') | |
| parser.add_argument('--port', type=int, default=9, | |
| help='UDP port (default: 9, alternative: 7)') | |
| args = parser.parse_args() | |
| try: | |
| success = wake_on_lan(args.mac, args.ip, args.port) | |
| except ValueError as e: | |
| print(f"Validation error: {e}", file=sys.stderr) | |
| sys.exit(1) | |
| except KeyboardInterrupt: | |
| print("\nAborted", file=sys.stderr) | |
| sys.exit(130) | |
| else: | |
| sys.exit(0 if success else 1) | |
| if __name__ == "__main__": | |
| main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment