-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmassresolv.go
69 lines (50 loc) · 1.26 KB
/
massresolv.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
//return a list of all hosts in an ip range with hostname
package main
import (
"fmt"
"net"
"os"
"encoding/binary"
"time"
)
func main(){
if len(os.Args) != 2{
fmt.Println("Provide network/bits as input")
os.Exit(1)
}
//parse ip to *ipnet struct
_, ipv4Net,err := net.ParseCIDR(os.Args[1])
if err != nil{
fmt.Println(err)
os.Exit(-1)
}
// convert IPNet struct mask and address to uint32
mask := binary.BigEndian.Uint32(ipv4Net.Mask)
start := binary.BigEndian.Uint32(ipv4Net.IP)
finish := (start & mask) | (mask ^ 0xffffffff)
startTime:= time.Now()
//create channel
ch := make(chan string)
for i := start; i <= finish; i++{
ip := make(net.IP, 4)
binary.BigEndian.PutUint32(ip,i)
go resolv(ip.String(),ch)
}
//wait for output from channel
for i := start; i <= finish; i++{
fmt.Printf(<-ch)
}
fmt.Printf("%.2fs elapsed\n",time.Since(startTime).Seconds())
}
//resolve ip address from range of hosts
func resolv(ip string, ch chan <- string){
start := time.Now()
names, err := net.LookupAddr(ip)
//return empty output (so itterator does not get stuck)
if err !=nil || len(names) == 0{
ch <- fmt.Sprint()
return
}
stop:=time.Since(start).Seconds()
ch <- fmt.Sprintf("%s resolved to %s in %.2fs\n",ip,names,stop)
}