main.go
1.06 KB
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
package main
import (
"bufio"
"fmt"
"io"
"io/ioutil"
"log"
"os"
"os/exec"
)
func main() {
var host string
var out io.Writer
if l := len(os.Args); l == 1 {
b, err := ioutil.ReadFile("host.iperf")
if err != nil {
host = "172.30.0.1"
} else {
host = string(b)
}
out = os.Stdout
} else if l == 2 {
host = os.Args[1]
out = os.Stdout
} else {
f, err := os.OpenFile(os.Args[2], os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0666)
if err != nil {
log.Fatal(err)
}
defer f.Close()
host = os.Args[1]
out = f
}
test("hw-wifi", host, out)
}
func test(code, host string, out io.Writer) {
c := exec.Command("iperf", "-c", host, "-i", "1", "-t", "10")
stdout, err := c.StdoutPipe()
err = c.Start()
if err != nil {
log.Fatal(err)
}
state := 0
s := bufio.NewScanner(stdout)
for s.Scan() {
fmt.Fprintf(out, "{\"code\": \"%v\", \"state\": %v, \"data\": \"%v\", \"timeout\": 10}\n", code, state, s.Text())
if state < 90 {
state += 10
}
}
fmt.Fprintf(out, "{\"code\": \"%v\", \"state\": 100, \"data\": \"\", \"timeout\": 10}\n", code)
}