Skip to content

Commit 8af7a53

Browse files
authored
Merge pull request #4 from misaka4e21/master
Implement receiving commandline arguments.
2 parents 9bb5967 + 5ed1470 commit 8af7a53

1 file changed

Lines changed: 44 additions & 21 deletions

File tree

lmap.go

Lines changed: 44 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package main
22

33
import (
4+
"os"
5+
"flag"
6+
"sort"
47
"bytes"
58
"encoding/binary"
69
"fmt"
@@ -12,8 +15,6 @@ import (
1215

1316
var icmp ICMP
1417

15-
var wg sync.WaitGroup
16-
1718
type ICMP struct {
1819
Type uint8
1920
Code uint8
@@ -23,41 +24,64 @@ type ICMP struct {
2324
}
2425

2526
func main() {
26-
wg.Add(1)
27-
go func() {
28-
CheckIP()
29-
wg.Done()
30-
}()
31-
wg.Wait()
32-
CheckIP()
27+
isVerbose := false
28+
flag.BoolVar(&isVerbose, "v", false, "be verbose")
29+
flag.Parse()
30+
args := flag.Args()
31+
if len(args) < 1 {
32+
fmt.Printf("使用方法:%s [-v] <网络号>/<CIDR>\n", os.Args[0])
33+
os.Exit(-1)
34+
}
35+
CheckIP(args[0], isVerbose)
3336
}
3437

35-
func CheckIP() {
38+
func CheckIP(subnet string, isVerbose bool) {
39+
var usedIP []string
40+
var unusedIP []string
3641
checkerGroup := &sync.WaitGroup{}
3742
t := time.Now()
38-
hosts, _ := hosts("10.150.1.1/24")
39-
usedIP := make([]string, 0, len(hosts))
40-
unusedIP := make([]string, 0, len(hosts))
43+
hosts, _ := getAllHostsFromCIDR(subnet)
4144
for _, ip := range hosts {
45+
time.Sleep(500)
4246
checkerGroup.Add(1)
4347
go func(data string) {
44-
pong := ping(data)
45-
if pong {
48+
defer checkerGroup.Done()
49+
isUsed := ping(data)
50+
if isUsed {
4651
usedIP = append(usedIP, data)
52+
if isVerbose {
53+
fmt.Println("已使用IP:", usedIP)
54+
}
4755
} else {
4856
unusedIP = append(unusedIP, data)
57+
if isVerbose {
58+
fmt.Println("未使用IP:", unusedIP)
59+
}
4960
}
50-
checkerGroup.Done()
5161
}(ip)
5262
}
5363
checkerGroup.Wait()
54-
fmt.Println("已使用IP:", usedIP)
55-
fmt.Println("未使用IP:", unusedIP)
5664
elapsed := time.Since(t)
5765
fmt.Println("IP扫描完成,耗时", elapsed)
66+
fmt.Println("已使用IP:", sortIPList(usedIP))
67+
fmt.Println("未使用IP:", sortIPList(unusedIP))
68+
}
69+
70+
func sortIPList(ipStrings []string) (result []string) {
71+
var ips []net.IP
72+
for _, ipString := range ipStrings {
73+
ips = append(ips, net.ParseIP(ipString))
74+
}
75+
sort.Slice(ips, func(i, j int) bool {
76+
return bytes.Compare(ips[i], ips[j]) < 0
77+
})
78+
for _, ip := range ips {
79+
result = append(result, ip.String())
80+
}
81+
return
5882
}
5983

60-
func hosts(cidr string) ([]string, error) {
84+
func getAllHostsFromCIDR(cidr string) ([]string, error) {
6185
ip, ipnet, err := net.ParseCIDR(cidr)
6286
if err != nil {
6387
return nil, err
@@ -97,8 +121,7 @@ func ping(ip string) bool {
97121
buffer.Reset()
98122
binary.Write(&buffer, binary.BigEndian, icmp)
99123

100-
Time, _ := time.ParseDuration("1s")
101-
conn, err := net.DialTimeout("ip4:icmp", ip, Time)
124+
conn, err := net.DialTimeout("ip4:icmp", ip, 1 * time.Second)
102125
if err != nil {
103126
return false
104127
}

0 commit comments

Comments
 (0)