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) }