Skip to content

Instantly share code, notes, and snippets.

@dgv
Created October 29, 2012 22:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dgv/3976842 to your computer and use it in GitHub Desktop.
Save dgv/3976842 to your computer and use it in GitHub Desktop.
Go Wake-On-Lan
package main
import (
"net"
"os"
"strings"
"encoding/hex"
"fmt"
)
func main() {
if len(os.Args) != 2 {
fmt.Fprintf(os.Stderr, "Usage: %s 00:01:02:03:04:05\n", os.Args[0])
os.Exit(1)
} else {
if len(os.Args[1]) != 17 {
fmt.Fprintf(os.Stderr, "%s invalid mac!\n", os.Args[1])
os.Exit(1)
}
}
BROADCAST_IPv4 := net.IPv4(255, 255, 255, 255)
socket,_ := net.DialUDP("udp4", nil, &net.UDPAddr{BROADCAST_IPv4,9})
buf := make([]byte,102)
buf = []byte{'\xFF','\xFF','\xFF','\xFF','\xFF','\xFF'}
// parse
mac1 := strings.Split(os.Args[1],":")
buf_mac := make([]byte, len(mac1))
for i:=0;i<len(mac1);i++ {
mac2,_ := hex.DecodeString(mac1[i])
buf_mac[i] = mac2[0]
}
// append
for i:=0;i<16;i++ {
buf = append(buf, buf_mac...)
}
// send
fmt.Printf("Wake-On-Lan packet sent to MAC address %s\n", os.Args[1])
socket.Write(buf)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment