golang併發ping主機

大囚長發表於2018-07-30

利用了golang對高併發的良好支援,同目錄下將ip每行一個寫入pinglist.txt檔案即可
其實這個功能用linux一條命令就能搞定:
cat pinglist.txt | xargs -P 10 -I {} ping -fc 100 {}

package main

import (
    "bufio"
    "bytes"
    "fmt"
    "io"
    "io/ioutil"
    "log"
    "os"
    "os/exec"
    "strings"
    "sync"
    "runtime"
)

func RunCMD(command string) string {
    in := bytes.NewBuffer(nil)
    cmd := exec.Command("sh")
    cmd.Stdin = in
    in.WriteString(command + "\n")
    stdout, err := cmd.StdoutPipe()
    if err != nil {
        log.Fatal(err)
    }
    defer stdout.Close()
    if err := cmd.Start(); err != nil {
        log.Fatal(err)
    }
    opBytes, err := ioutil.ReadAll(stdout)
    if err != nil {
        log.Fatal(err)
    }
    return string(opBytes)
}

func ping(ip string) string {
    cmd := "ping -fc 100 " + ip
    return RunCMD(cmd)
}

var ips = []string{}

func main() {
    runtime.GOMAXPROCS(runtime.NumCPU())
    fileName := "pinglist.txt"
    f, err := os.Open(fileName)
    if err != nil {
        fmt.Println(err)
    }
    buf := bufio.NewReader(f)
    for {
        line, err := buf.ReadString('\n')
        line = strings.TrimSpace(line)
        if err != nil {
            if err == io.EOF {
                //fmt.Println("File read over!")
                break
            }
            fmt.Println(err)
        }
        ips = append(ips, line)
    }
    wg := &sync.WaitGroup{}
    ch := make(chan string, len(ips))
    //fmt.Println(ips)
    for i:= 0; i < len(ips); i++ {
        wg.Add(1)
        go func(i int) {
            ch <- ping(ips[i])
            wg.Done()
        }(i)
    }
    wg.Wait()

    //fmt.Println(len(ch))
    for i := 0; i < len(ips); i++ {
        fmt.Println(<-ch)
    }
}

相關文章