-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
132 lines (117 loc) · 3.04 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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
// Command cablemodemcli is a tool to query the cable modem.
package main
import (
"encoding/json"
"flag"
"fmt"
"math/rand"
"os"
"time"
"github.com/tuxdude/cablemodemutil"
)
const (
minRandDelaySeconds = 5 * 60
maxRandDelaySeconds = 30 * 60
)
// Returns the JSON formatted string representation of the specified object.
func prettyPrintJSON(x interface{}) string {
p, err := json.MarshalIndent(x, "", " ")
if err != nil {
return fmt.Sprintf("%#v", x)
}
return string(p)
}
func randDelay() time.Duration {
max := maxRandDelaySeconds
min := minRandDelaySeconds
d := rand.Intn(max-min) + min
return time.Duration(d) * time.Second
}
func handleErr(err error) int {
fmt.Fprintf(os.Stderr, "Error: %s", err)
return 1
}
func runInFileMode() int {
f, err := os.ReadFile(*readFromFile)
if err != nil {
return handleErr(err)
}
var raw cablemodemutil.CableModemRawStatus
err = json.Unmarshal(f, &raw)
if err != nil {
return handleErr(err)
}
status, err := cablemodemutil.ParseRawStatus(raw)
if err != nil {
return handleErr(err)
}
if *showOutput {
fmt.Println(prettyPrintJSON(status))
}
return 0
}
func run() int {
if *readFromFile != "" {
return runInFileMode()
}
input := cablemodemutil.RetrieverInput{
Host: *host,
Protocol: *protocol,
SkipVerifyCert: *skipVerifyCert,
Username: *username,
ClearPassword: *password,
}
input.Debug.Debug = *debug
input.Debug.DebugReq = *debugReq
input.Debug.DebugResp = *debugResp
cm := cablemodemutil.NewStatusRetriever(&input)
beginTime := time.Now()
nextRequestTime := beginTime
if *loop == 0 {
fmt.Fprintln(os.Stderr, "Warning: -loop flag set to zero, hence not querying status from the cable modem.")
}
forever := *loop < 0
pending := *loop
for forever || pending > 0 {
pending--
currTime := time.Now()
if *delay < 0 {
nextRequestTime = nextRequestTime.Add(randDelay())
} else {
nextRequestTime = nextRequestTime.Add(time.Duration(*delay) * time.Second)
}
if *debug {
fmt.Printf("\n\n************************************************\n")
fmt.Printf("Begin time:%s\n", beginTime)
fmt.Printf("Status Query time: %s\n", currTime)
fmt.Printf("************************************************\n\n")
}
status, err := cm.Status()
if err != nil {
return handleErr(err)
}
if *showOutput {
fmt.Println(prettyPrintJSON(status))
}
if *debug {
if forever || pending > 0 {
fmt.Printf("\n\n************************************************\n")
fmt.Printf("Begin time:%s\n", beginTime)
fmt.Printf("Last Query time: %s\n", currTime)
fmt.Printf("Next Query time: %s\n", nextRequestTime)
fmt.Printf("************************************************\n\n")
time.Sleep(time.Until(nextRequestTime))
} else {
fmt.Printf("\n\n************************************************\n")
fmt.Printf("Begin time:%s\n", beginTime)
fmt.Printf("Last Query time: %s\n", currTime)
fmt.Printf("************************************************\n\n")
}
}
}
return 0
}
func main() {
flag.Parse()
os.Exit(run())
}