-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
105 lines (89 loc) · 2.05 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package main
import (
"flag"
"fmt"
"log"
"net"
"os"
)
var (
cliPassword string
cliAddr string
cliPort int
cliHelp bool
cliVersion bool
)
func init() {
flag.StringVar(&cliPassword, "pw", "00-00-00-00-00-00", "set magic packet password")
flag.StringVar(&cliAddr, "a", "255.255.255.255", "set broadcast IP")
flag.IntVar(&cliPort, "p", 9, "set udp port")
flag.BoolVar(&cliHelp, "h", false, "show help")
flag.BoolVar(&cliVersion, "v", false, "show version")
flag.Parse()
// 判断IP是否合法
if ip := net.ParseIP(cliAddr); ip == nil {
log.Fatalf("%s is not a valid host", cliAddr)
}
// 判断端口是否合法
if cliPort < 0 || cliPort > 65535 {
log.Fatalf("%d is not a valid port", cliPort)
}
// 重写显示用法函数
flag.Usage = func() {
var helpInfo = `Usage:
wakeonlan {Command} [Option] MAC_ADDRESS
Command:
-h : show help
-v : show version
Option:
-pw <Password> : set magic packet password
-a <IP> : set broadcast IP
-p <Port> : set udp port
Example:
1) wakeonlan 1A-2B-3C-4D-5E-6F
2) wakeonlan -pw AA-BB-CC-DD-EE-FF -a 192.168.1.255 -p 9 1A-2B-3C-4D-5E-6F
3) wakeonlan -h
4) wakeonlan -v`
fmt.Println(helpInfo)
}
// 如果无 args 或者 指定 h 参数,打印用法后退出
if len(os.Args) == 1 || cliHelp {
flag.Usage()
os.Exit(0)
}
// 打印版本信息
if cliVersion {
showVersion()
os.Exit(0)
}
}
func showVersion() {
var versionInfo = `v1.03`
fmt.Println(versionInfo)
}
func showChangelog() {
var versionInfo = `Changelog:
1.00:
- First release
1.01:
- Modify fmt.Errorf() to avoid failure if the password is not a string
1.02:
- Change terminal command
1.03:
- Modify readme`
fmt.Println(versionInfo)
}
func main() {
if len(flag.Args()) > 0 {
mac := flag.Args()[0]
magicPacket, err := ParseMagicPacket(mac, cliPassword)
if err != nil {
log.Fatal(err)
}
err = magicPacket.Send(cliAddr, cliPort)
if err != nil {
log.Fatal(err)
}
fmt.Println("magic packet send successfully!")
}
}