diff src/magicpacket.go @ 33:90659c7171ed

add magicpacket.go
author pyon@macmini
date Tue, 13 Aug 2019 19:55:10 +0900
parents
children 523416ca4b83
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/magicpacket.go	Tue Aug 13 19:55:10 2019 +0900
@@ -0,0 +1,37 @@
+package main
+
+import (
+	"bytes"
+	"fmt"
+	"net"
+	"log"
+	"os"
+)
+
+func main() {
+	if len(os.Args) < 2 {
+		fmt.Fprintln(os.Stderr, "wol mac-address1 mac-address2 ...")
+		os.Exit(1)
+	}
+	for i, m := range os.Args {
+		if i == 0 {
+			continue
+		}
+		hw, err := net.ParseMAC(m)
+		if err != nil {
+			log.Fatal(err)
+		}
+
+		b := [][]byte{bytes.Repeat([]byte{255}, 6), bytes.Repeat([]byte(hw), 16)}
+		pkt := bytes.Join(b, nil)
+
+		conn, err := net.Dial("udp", "255.255.255.255:9")
+		if err != nil {
+			log.Fatal(err)
+		}
+		defer conn.Close()
+
+		conn.Write(pkt)
+	}
+}
+