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)
}
}
以上這篇golang并發(fā)ping主機(jī)的方法就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。