Skip to content

Instantly share code, notes, and snippets.

@imposibrus
Forked from SteveMarshall/send-magic-packet.sh
Last active September 4, 2022 04:15
Show Gist options
  • Save imposibrus/b75db3e6a47bf22176713f4f2ed67d5f to your computer and use it in GitHub Desktop.
Save imposibrus/b75db3e6a47bf22176713f4f2ed67d5f to your computer and use it in GitHub Desktop.
Wake-On-Lan Magic Packet using netcat in bash
#!/usr/bin/env bash
mac_address=$1
# Strip colons from the MAC address
mac_address=$(echo $mac_address | sed 's/://g')
broadcast=$2
port=9
nc_args="-w1 -u"
if [[ $OSTYPE == darwin* ]]; then
nc_args="$params -c"
fi
# Magic packets consist of 12*`f` followed by 16 repetitions of the MAC address
magic_packet=$(
printf 'f%.0s' {1..12}
printf "$mac_address%.0s" {1..16}
)
# ... and they need to be hex-escaped
magic_packet=$(
echo $magic_packet | sed -e 's/../\\x&/g'
)
echo -e $magic_packet | nc $nc_args $broadcast $port
@imposibrus
Copy link
Author

imposibrus commented Jan 23, 2018

Requirements

If you planning send packet over internet, then open port 9 on your router and forward UDP packets from it to 192.168.1.254 (any unused ip).
zyx_start_wol

Also, you should add static ARP record on router (telnet command for Zyxel Keenetic Start):

ip arp 192.168.1.254 ff:ff:ff:ff:ff:ff
copy running-config startup-config # saves config

After this all packets from internet to UDP/9 port will forward to 192.168.1.254 and acts like broadcast packets.

Usage

send-magic-packet.sh aa:bb:cc:dd:ee:ff 192.168.1.11

Credits

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