-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
99 lines (92 loc) · 2.27 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package main
import (
"bufio"
"flag"
"fmt"
"io"
"log"
"os"
"regexp"
"sync"
"github.com/chyuhung/goping/Host"
)
func isTrue(i int) bool {
if i == 0 {
return false
}
return true
}
func main() {
var wg sync.WaitGroup
var hostList []*Host.Host
//输入ip文件
var input = flag.String("i", "ip.txt", "Input file.")
//输出结果文件
var output = flag.String("o", "result.txt", "Output file.")
//最大协程数量
var maxNum = flag.Int("m", 10, "The number of goroutines, the maximum is the number of IPs.")
//ping包数量
var pingNum = flag.Int("n", 1, "Number of messages per ping.")
var isICMP = flag.Int("t", 1, "Set the type of ping pinger will send.\n"+
"False means pinger will send an \"unprivileged\" UDP ping.\n"+
"True means pinger will send a \"privileged\" raw ICMP ping.\n"+
"NOTE: setting to true requires that it be run with super-user privileges.")
flag.Parse()
fmt.Println("Goroutines:", *maxNum)
fmt.Println("Messages:", *pingNum)
fmt.Println("Input file:", *input)
fmt.Println("Output file:", *output)
fmt.Println("Send privileged ping:", *isICMP, "\n")
var maxg chan struct{} //限制最大协程数
ipList := getIPList(*input)
count := len(*ipList) //获取ip数量
if count <= *maxNum {
maxg = make(chan struct{}, count)
} else {
maxg = make(chan struct{}, *maxNum)
count = *maxNum
}
for i := count; i > 0; i-- {
maxg <- struct{}{}
}
for _, ip := range *ipList {
h := Host.NewHost(ip)
hostList = append(hostList, h)
wg.Add(1)
go h.Run(&wg, *pingNum, isTrue(*isICMP), maxg)
}
wg.Wait()
f, err := os.Create(*output)
if err != nil {
log.Println(err)
os.Exit(1)
}
defer f.Close()
for i := range hostList {
_, err := f.WriteString(hostList[i].Ipaddr + " " + hostList[i].Reachable + "\n")
if err != nil {
log.Fatal(err)
}
}
}
func getIPList(filepath string) *[]string {
file, err := os.Open(filepath)
if err != nil {
log.Fatal(err)
}
defer file.Close()
reg := regexp.MustCompile(`^((0|[1-9]\d?|1\d\d|2[0-4]\d|25[0-5])\.){3}(0|[1-9]\d?|1\d\d|2[0-4]\d|25[0-5])$`)
var ipList []string
reader := bufio.NewReader(file)
for {
line, _, err := reader.ReadLine()
if err == io.EOF {
break
}
strLine := string(line)
if reg.MatchString(strLine) {
ipList = append(ipList, strLine)
}
}
return &ipList
}