Created
October 29, 2012 22:03
-
-
Save dgv/3976842 to your computer and use it in GitHub Desktop.
Go 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
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