File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ module github.com/LinuxHub-Group/lmap
2+
3+ go 1.17
Original file line number Diff line number Diff line change @@ -24,31 +24,35 @@ type ICMP struct {
2424
2525func 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
3235func 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
121121func CheckSum (data []byte ) uint16 {
You can’t perform that action at this time.
0 commit comments