Skip to content

Commit 7409655

Browse files
author
coolrc136
committed
♻️ 使用单个切片来处理ip列表
1 parent cd713e7 commit 7409655

4 files changed

Lines changed: 39 additions & 38 deletions

File tree

cmd/lmap/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ func main() {
3131
flag.Parse()
3232
args := flag.Args()
3333
if len(args) < 1 {
34-
fmt.Fprintf(os.Stderr, "使用方法:%s [-v] <网络号>/<CIDR>\n", os.Args[0])
34+
_, _ = fmt.Fprintf(os.Stderr, "使用方法:%s [-v] <网络号>/<CIDR>\n", os.Args[0])
3535
os.Exit(-1)
3636
}
3737
lmap.CheckIP(args[0], isVerbose)

pkg/lmap/check_ip.go

Lines changed: 22 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -20,64 +20,55 @@ package lmap
2020

2121
import (
2222
"fmt"
23-
"net"
2423
"os"
2524
"sync"
2625
"time"
2726
)
2827

29-
const OUTPUT_IP_PER_LINE = 5
28+
const OUTPUT_IP_PER_LINE = 3
3029

3130
func CheckIP(subnet string, isVerbose bool) {
3231
checkerGroup := &sync.WaitGroup{}
3332
t := time.Now()
3433
hosts, _ := GetAllIPsFromCIDR(subnet)
35-
isUsedList := make([]bool, len(hosts))
36-
for index, _ := range hosts {
34+
for index := range hosts {
3735
time.Sleep(500)
3836
checkerGroup.Add(1)
37+
3938
go func(index int) {
4039
defer checkerGroup.Done()
41-
isUsed := Ping(hosts[index])
42-
isUsedList[index] = isUsed
40+
hosts[index].isUsed = Ping(hosts[index].host)
4341
if isVerbose {
44-
if isUsed {
45-
println("已使用IP:", hosts[index].String())
42+
if hosts[index].isUsed {
43+
println("已使用IP:", hosts[index].host.String())
4644
} else {
47-
println("未使用IP:", hosts[index].String())
45+
println("未使用IP:", hosts[index].host.String())
4846
}
4947
}
5048
}(index)
5149
}
5250
checkerGroup.Wait()
5351
elapsed := time.Since(t)
54-
fmt.Fprintln(os.Stderr, "IP扫描完成,耗时", elapsed)
55-
println("已使用IP:")
56-
printIPList(hosts, true, isUsedList)
57-
println("未使用IP:")
58-
printIPList(hosts, false, isUsedList)
52+
_, _ = fmt.Fprintln(os.Stderr, "IP扫描完成,耗时", elapsed)
53+
fmt.Println("已使用IP:")
54+
printIPList(hosts, true)
55+
fmt.Println("未使用IP:")
56+
printIPList(hosts, false)
5957
}
6058

61-
func printIPList(hosts []net.IP, boolFilter bool, boolFilterTargetList []bool) {
62-
firstIndex := 0
63-
count := 0
64-
for index, ip := range hosts {
65-
if boolFilterTargetList[index] == boolFilter {
66-
fmt.Print(ip.String())
67-
firstIndex = index
68-
count = 1
69-
break
70-
}
71-
}
72-
for index := firstIndex + 1; index < len(hosts); index++ {
73-
if boolFilterTargetList[index] == boolFilter {
74-
if count%OUTPUT_IP_PER_LINE == 0 {
75-
fmt.Println("")
59+
func printIPList(hosts []HostInfo, boolFilter bool) {
60+
61+
position := 1
62+
63+
for _,hostInfo :=range hosts{
64+
if boolFilter==hostInfo.isUsed {
65+
fmt.Print(hostInfo.host.String())
66+
if position%OUTPUT_IP_PER_LINE == 0 {
67+
fmt.Println()
7668
} else {
7769
fmt.Print(", ")
7870
}
79-
fmt.Print(hosts[index].String())
80-
count++
71+
position++
8172
}
8273
}
8374
fmt.Println("")

pkg/lmap/common.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,17 @@
1818

1919
package lmap
2020

21+
import "net"
22+
2123
type ICMP struct {
2224
Type uint8
2325
Code uint8
2426
Checksum uint16
2527
Identifier uint16
2628
SequenceNum uint16
2729
}
30+
31+
type HostInfo struct {
32+
host net.IP
33+
isUsed bool
34+
}

pkg/lmap/parse_subnet.go

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,20 +20,23 @@ package lmap
2020

2121
import "net"
2222

23-
func GetAllIPsFromCIDR(cidr string) ([]net.IP, error) {
24-
ip, ipnet, err := net.ParseCIDR(cidr)
23+
func GetAllIPsFromCIDR(cidr string) ([]HostInfo, error) {
24+
ip, ipNet, err := net.ParseCIDR(cidr)
2525
if err != nil {
2626
return nil, err
2727
}
2828

29-
var ips []net.IP
30-
for ip := ip.Mask(ipnet.Mask); ipnet.Contains(ip); inc(ip) {
31-
ips = append(ips, dupIP(ip))
29+
var ips []HostInfo
30+
for ip := ip.Mask(ipNet.Mask); ipNet.Contains(ip); inc(ip) {
31+
ips = append(ips, HostInfo{
32+
host: dupIP(ip),
33+
isUsed: false,
34+
})
3235
}
3336
if len(ips) <= 2 {
3437
return ips, nil
3538
}
36-
return ips[1 : len(ips)-1], nil
39+
return ips[0 : len(ips)-1], nil
3740
}
3841

3942
func inc(ip net.IP) {

0 commit comments

Comments
 (0)