Created
March 27, 2020 21:18
-
-
Save weldpua2008/c7cd60e2423a4fe8622852a5bd6ddb12 to your computer and use it in GitHub Desktop.
Revisions
-
weldpua2008 created this gist
Mar 27, 2020 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,41 @@ package main import ( "bufio" "os" "os/exec" "fmt" "log" "io" ) func main() { cmd := exec.Command("bash","-c","ping -c 2 127.0.0.1") // cmd := exec.Command("bash","-c","'id'") // cmd := exec.Command("ping", "127.0.0.1") stdout, err := cmd.StdoutPipe() if err != nil { log.Fatal(err) } cmd.Start() buf := bufio.NewReader(stdout) // Notice that this is not in a loop num := 1 for { line, err := buf.ReadString('\n') if err == io.EOF { break } if err != nil && err != io.EOF { fmt.Println(err) os.Exit(1) } // line, _, _ := buf.ReadLine() // if num > 300 { // os.Exit(0) // } num += 1 fmt.Println(string(line)) } }