Skip to content

Instantly share code, notes, and snippets.

@HuakunShen
Last active March 13, 2021 07:38
Show Gist options
  • Save HuakunShen/25460c3b4442442e8148545804395b62 to your computer and use it in GitHub Desktop.
Save HuakunShen/25460c3b4442442e8148545804395b62 to your computer and use it in GitHub Desktop.
wakeonlan
package main
import (
"encoding/hex"
"fmt"
"net"
"strings"
)
func main() {
mac := "000c29100678"
port := 9
ip := "255.255.255.255"
macRepeat := strings.Repeat(mac, 16) // 16 * 6 = 96 bytes
macBytes, err := hex.DecodeString(macRepeat)
if err != nil {
panic(err)
}
packet := make([]byte, 0, 102) // 6+16*6=102 (mac address is 6 bytes too)
packet = append(packet, []byte(strings.Repeat("\xFF", 6))...)
packet = append(packet, macBytes...)
conn, err := net.Dial("udp", fmt.Sprintf("%v:%d", ip, port))
if err != nil {
panic(err)
}
if n, err := conn.Write(packet); err != nil && n != 102 {
panic(fmt.Errorf("magic packet sent was %d bytes (expected 102 bytes sent)", n))
}
if err = conn.Close(); err != nil {
panic(err)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment