Skip to content

Instantly share code, notes, and snippets.

@weldpua2008
Created March 27, 2020 21:18
Show Gist options
  • Select an option

  • Save weldpua2008/c7cd60e2423a4fe8622852a5bd6ddb12 to your computer and use it in GitHub Desktop.

Select an option

Save weldpua2008/c7cd60e2423a4fe8622852a5bd6ddb12 to your computer and use it in GitHub Desktop.

Revisions

  1. weldpua2008 created this gist Mar 27, 2020.
    41 changes: 41 additions & 0 deletions ping.go
    Original 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))
    }
    }