Skip to content

Commit a3623bd

Browse files
DESKTOP-OB8V0P7\AdministratorDESKTOP-OB8V0P7\Administrator
authored andcommitted
正对#16优化
1 parent b9989b7 commit a3623bd

3 files changed

Lines changed: 73 additions & 21 deletions

File tree

cmd/lmap/main.go

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,30 @@ import (
2929
func main() {
3030
isVerbose := false
3131
flag.BoolVar(&isVerbose, "v", false, "be verbose")
32+
33+
subnets := multiArg{}
34+
flag.Var(&subnets, "subnet", "network to scan (can be specified multiple times)")
35+
36+
excludes := multiArg{}
37+
flag.Var(&excludes, "exclude", "IP or subnet to exclude (can be specified multiple times)")
38+
3239
flag.Parse()
33-
args := flag.Args()
34-
if len(args) < 1 {
35-
_, _ = fmt.Fprintf(os.Stderr, "使用方法:%s [-v] <网络号>/<CIDR>\n", os.Args[0])
40+
41+
if len(subnets) < 1 {
42+
_, _ = fmt.Fprintf(os.Stderr, "使用方法:%s [-v] -subnet <网络号>/<CIDR> [-subnet ...] [-exclude <IP>/<CIDR>] [-exclude ...]\n", os.Args[0])
3643
os.Exit(-1)
3744
}
38-
lmap.CheckIP(args[0], isVerbose)
45+
46+
lmap.CheckIP(subnets, excludes, isVerbose)
47+
}
48+
49+
type multiArg []string
50+
51+
func (m *multiArg) String() string {
52+
return fmt.Sprintf("%v", *m)
53+
}
54+
55+
func (m *multiArg) Set(value string) error {
56+
*m = append(*m, value)
57+
return nil
3958
}

pkg/lmap/check_ip.go

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,33 +27,39 @@ import (
2727

2828
const OUTPUT_IP_PER_LINE = 3
2929

30-
func CheckIP(subnet string, isVerbose bool) {
30+
func CheckIP(subnets, excludes []string, isVerbose bool) {
3131
checkerGroup := &sync.WaitGroup{}
3232
t := time.Now()
33-
hosts, _ := GetAllIPsFromCIDR(subnet)
34-
for index := range hosts {
35-
//time.Sleep(500)
33+
34+
var allHosts []HostInfo
35+
for _, subnet := range subnets {
36+
hosts, _ := GetAllIPsFromCIDR(subnet, excludes)
37+
allHosts = append(allHosts, hosts...)
38+
}
39+
40+
for index := range allHosts {
3641
checkerGroup.Add(1)
3742

3843
go func(index int) {
3944
defer checkerGroup.Done()
40-
hosts[index].isUsed = Ping(hosts[index].host)
45+
allHosts[index].isUsed = Ping(allHosts[index].host)
4146
if isVerbose {
42-
if hosts[index].isUsed {
43-
fmt.Println("已使用IP:", hosts[index].host.String())
47+
if allHosts[index].isUsed {
48+
fmt.Println("已使用IP:", allHosts[index].host.String())
4449
} else {
45-
fmt.Println("未使用IP:", hosts[index].host.String())
50+
fmt.Println("未使用IP:", allHosts[index].host.String())
4651
}
4752
}
4853
}(index)
4954
}
55+
5056
checkerGroup.Wait()
5157
elapsed := time.Since(t)
5258
_, _ = fmt.Fprintln(os.Stderr, "IP扫描完成,耗时", elapsed)
5359
fmt.Println("已使用IP:")
54-
printIPList(hosts, true)
60+
printIPList(allHosts, true)
5561
fmt.Println("未使用IP:")
56-
printIPList(hosts, false)
62+
printIPList(allHosts, false)
5763
}
5864

5965
func printIPList(hosts []HostInfo, boolFilter bool) {

pkg/lmap/parse_subnet.go

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,27 +18,55 @@
1818

1919
package lmap
2020

21-
import "net"
21+
import (
22+
"net"
23+
"strings"
24+
)
2225

23-
func GetAllIPsFromCIDR(cidr string) ([]HostInfo, error) {
26+
func GetAllIPsFromCIDR(cidr string, excludes []string) ([]HostInfo, error) {
2427
ip, ipNet, err := net.ParseCIDR(cidr)
2528
if err != nil {
2629
return nil, err
2730
}
2831

2932
var ips []HostInfo
3033
for ip := ip.Mask(ipNet.Mask); ipNet.Contains(ip); inc(ip) {
31-
ips = append(ips, HostInfo{
32-
host: dupIP(ip),
33-
isUsed: false,
34-
})
34+
ipStr := ip.String()
35+
if !isExcluded(ipStr, excludes) {
36+
ips = append(ips, HostInfo{
37+
host: dupIP(ip),
38+
isUsed: false,
39+
})
40+
}
3541
}
3642
if len(ips) <= 2 {
3743
return ips, nil
3844
}
3945
return ips[0 : len(ips)-1], nil
4046
}
4147

48+
func isExcluded(ip string, excludes []string) bool {
49+
for _, exclude := range excludes {
50+
if strings.Contains(exclude, "/") {
51+
_, excludeNet, err := net.ParseCIDR(exclude)
52+
if err != nil {
53+
//fmt.Printf("解析排除规则 %s 失败: %v\n", exclude, err)
54+
continue
55+
}
56+
if excludeNet.Contains(net.ParseIP(ip)) {
57+
//fmt.Printf("IP %s 被排除规则 %s 排除\n", ip, exclude)
58+
return true
59+
}
60+
} else {
61+
if ip == exclude {
62+
//fmt.Printf("IP %s 被排除规则 %s 排除\n", ip, exclude)
63+
return true
64+
}
65+
}
66+
}
67+
return false
68+
}
69+
4270
func inc(ip net.IP) {
4371
for j := len(ip) - 1; j >= 0; j-- {
4472
ip[j]++
@@ -49,7 +77,6 @@ func inc(ip net.IP) {
4977
}
5078

5179
func dupIP(ip net.IP) net.IP {
52-
// To save space, try and only use 4 bytes
5380
if x := ip.To4(); x != nil {
5481
ip = x
5582
}

0 commit comments

Comments
 (0)