Skip to content

Instantly share code, notes, and snippets.

@wiless
Created September 29, 2019 17:53
Show Gist options
  • Save wiless/2dbe17be39ea9bee95be8610db216a60 to your computer and use it in GitHub Desktop.
Save wiless/2dbe17be39ea9bee95be8610db216a60 to your computer and use it in GitHub Desktop.
iperf async cmd stream
package main
import (
"fmt"
"os"
"strings"
"time"
"github.com/go-cmd/cmd"
)
func main() {
// Disable output buffering, enable streaming
cmdOptions := cmd.Options{
Buffered: false,
Streaming: true,
}
fmt.Println("Executing .. : ", strings.Join([]string{"/usr/bin/iperf3", "-c", "192.168.1.101", "-u", "-i1", "-t5", "--forceflush"}, " "))
envCmd := cmd.NewCmdOptions(cmdOptions, "/usr/bin/iperf3", "-c", "192.168.1.101", "-u", "-i1", "-t5", "--forceflush")
// Create Cmd with options
// envCmd := cmd.NewCmdOptions(cmdOptions, "env")
// Print STDOUT and STDERR lines streaming from Cmd
go func() {
for {
select {
case line := <-envCmd.Stdout:
fmt.Println("line ", line)
case line := <-envCmd.Stderr:
fmt.Fprintln(os.Stderr, line)
}
}
}()
// Run and wait for Cmd to return, discard Status
<-envCmd.Start()
// Cmd has finished but wait for goroutine to print all lines
for len(envCmd.Stdout) > 0 || len(envCmd.Stderr) > 0 {
time.Sleep(10 * time.Millisecond)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment