Created
March 27, 2020 21:18
-
-
Save weldpua2008/c7cd60e2423a4fe8622852a5bd6ddb12 to your computer and use it in GitHub Desktop.
Run ping from go
This file contains 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 characters
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)) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment