forked from movsb/gpon-client
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
77 lines (70 loc) · 1.8 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
package main
import (
"fmt"
"log"
"os"
"strconv"
)
func main() {
if len(os.Args) == 1 {
usage(os.Args[0])
return
}
client := MustDial(
defEnv("IP", "192.168.1.1"),
defEnv("USERNAME", "useradmin"),
defEnv("PASSWORD", ""),
)
switch cmd := os.Args[1]; cmd {
default:
log.Fatalf("unknown command: %s\n", cmd)
case "reboot":
fmt.Println("restart in 10s...")
client.Reboot()
case "portmaps":
subcmd := os.Args[2]
a := os.Args[3:]
_ = a
switch subcmd {
default:
log.Fatalf("unknown sub-command: %s\n", subcmd)
case "list":
rules := client.ListPortMappings()
fmtstr := "%-5v%-16v%-12v%-12v%-20v%-12v%-6v\n"
fmt.Printf(fmtstr, "ID", "Name", "Protocol", "OuterPort", "InnerIP", "InnerPort", "Enable")
fmt.Println("-----------------------------------------------------------------------------------")
for i, r := range rules {
fmt.Printf(fmtstr, i+1, r.Name, r.Protocol, r.OuterPort, r.InnerIP, r.InnerPort, r.Enable)
}
case "create":
name := a[0]
protocol := a[1]
outerPort, _ := strconv.Atoi(a[2])
innerIP := a[3]
innerPort, _ := strconv.Atoi(a[4])
client.CreatePortMapping(name, protocol, outerPort, innerIP, innerPort)
case "delete":
name := a[0]
client.DeletePortMapping(name)
case "enable":
name := a[0]
client.EnablePortMapping(name, true)
case "disable":
name := a[0]
client.EnablePortMapping(name, false)
}
}
}
func usage(name string) {
usageText := `A GPON (Tiānyì Gateway) client used to modify router configurations.
Usage: %[1]s command [sub-command] [<arguments>...]
All Available Command List:
reboot now
portmaps list
portmaps create <name> <protocol> <outer-port> <inner-ip> <inner-port>
portmaps delete <name>
portmaps enable <name>
portmaps disable <name>
`
fmt.Fprintf(os.Stderr, usageText, name)
}