Skip to content

Commit 042033c

Browse files
committed
performances enhancements
1 parent 7853705 commit 042033c

2 files changed

Lines changed: 20 additions & 17 deletions

File tree

go.mod

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module github.com/LinuxHub-Group/lmap
2+
3+
go 1.17

lmap.go

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -24,31 +24,35 @@ type ICMP struct {
2424

2525
func main() {
2626
wg.Add(1)
27-
go CheckIP()
28-
CheckIP()
27+
go func() {
28+
CheckIP()
29+
wg.Done()
30+
}()
2931
wg.Wait()
32+
CheckIP()
3033
}
3134

3235
func CheckIP() {
33-
defer wg.Done()
34-
var usedIP []string
35-
var unusedIP []string
36+
checkerGroup := &sync.WaitGroup{}
3637
t := time.Now()
3738
hosts, _ := hosts("10.150.1.1/24")
39+
usedIP := make([]string, 0, len(hosts))
40+
unusedIP := make([]string, 0, len(hosts))
3841
for _, ip := range hosts {
39-
tmp := ip
42+
checkerGroup.Add(1)
4043
go func(data string) {
41-
bool := ping(data)
42-
if bool {
44+
pong := ping(data)
45+
if pong {
4346
usedIP = append(usedIP, data)
44-
fmt.Println("已使用IP:", usedIP)
4547
} else {
4648
unusedIP = append(unusedIP, data)
47-
fmt.Println("未使用IP:", unusedIP)
4849
}
49-
}(tmp)
50+
checkerGroup.Done()
51+
}(ip)
5052
}
51-
wg.Wait()
53+
checkerGroup.Wait()
54+
fmt.Println("已使用IP:", usedIP)
55+
fmt.Println("未使用IP:", unusedIP)
5256
elapsed := time.Since(t)
5357
fmt.Println("IP扫描完成,耗时", elapsed)
5458
}
@@ -111,11 +115,7 @@ func ping(ip string) bool {
111115

112116
conn.SetReadDeadline(time.Time{})
113117

114-
if string(recvBuf[0:num]) != "" {
115-
return true
116-
}
117-
return false
118-
118+
return string(recvBuf[0:num]) != ""
119119
}
120120

121121
func CheckSum(data []byte) uint16 {

0 commit comments

Comments
 (0)