-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
68 lines (61 loc) · 1.47 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package main
import (
"context"
"fmt"
"io"
"time"
"github.com/mostlygeek/arp"
"github.com/pcrandall/networkscanner/network"
)
func main() {
// initialize vars
timeout := time.Duration(_timeout) * time.Millisecond
interval := time.Duration(_interval) * time.Millisecond
hostConn := make(map[string]*host)
pingedAddr := make(map[string]*onlineHosts)
_, IPrange := network.CalculateCIDR(ip)
// ping addresses
for _, ip := range IPrange {
openAddr.addAddr(ip) // build list of possible addresses
wg.Add(1)
ctx := context.Background()
hostConn[ip] = &host{
Address: ip,
Ctx: &ctx,
Count: count,
PingTime: timeout,
Interval: interval,
}
pingedAddr[ip] = &onlineHosts{}
go Ping(ip, hostConn[ip].Ctx, hostConn[ip], pingedAddr[ip])
}
// arp lookup
for ip, _ := range arp.Table() {
go func(ip string) {
mac := arp.Search(ip)
if mac != "" && mac != "00:00:00:00:00:00" { // remove from available only if valid mac address was returned
if debug {
fmt.Println(ip, "from arp.search() mac: ", mac)
}
pingedAddr[ip].mac = mac
openAddr.delAddr(ip)
}
}(ip)
}
// wait for all gofuncs to finish
wg.Wait()
// print & write online host table
if verbose {
printOnlineHosts(pingedAddr)
}
// delete excluded addresses
for _, ip := range exclude {
openAddr.delAddr(ip)
}
//write remaining available addresses to file.
if write {
for _, val := range openAddr.table {
io.WriteString(availableHostsFile, val+"\n")
}
}
}