Skip to content

Instantly share code, notes, and snippets.

@o-y
Last active November 11, 2023 03:20
Show Gist options
  • Save o-y/e34d662038ce6cb16d4d6c7d6e3b6b20 to your computer and use it in GitHub Desktop.
Save o-y/e34d662038ce6cb16d4d6c7d6e3b6b20 to your computer and use it in GitHub Desktop.
python script which observes networked Wake on Lan calls and forwards them.
-------------| Install deps
sudo apt install etherwake
pip3 install scapy
-------------| Allow unprivileged etherwake invocations
sudo ln -s /usr/sbin/etherwake /usr/bin/etherwake
sudo setcap cap_net_raw+ep /usr/sbin/etherwake
-------------| Get mac address
sudo iwconfig
OR
ip addr show
OR
sudo arp --verbose
-------------| Create:
from scapy.all import *
mac_address = "<< MAC ADDRESS HERE >>"
command = f"etherwake -i eth0 {mac_address}"
def wake_on_lan(packet):
# NOTE: This doesn't do any validation of the packet (e.g not verifying it's a magic packet),
# which is fine as this is on a private tailscale network, but might not be okay for all
# use cases.
print("received wake_on_lan packet", packet)
try:
subprocess.run(command, shell=True, check=True)
print(f"wol signal sent to {mac_address}")
except subprocess.CalledProcessError as e:
print(f"subprocess failed. error: {e}")
sniff(prn=wake_on_lan, filter="udp and port 9", store=0)
-------------| Execute:
sudo -E python3 wol.py
-E preserves the caller environment which is required as pip dependencies shouldn't be called from elevated contexts
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment