-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
63 lines (57 loc) · 1.43 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
// Copyright 2021 Rafal Glinski. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Package implements functions for bulk IP address DNS lookup.
// It uses goroutines to speed up the process.
package main
import (
"bufio"
"flag"
"fmt"
"net"
"os"
"strings"
)
var (
errVal = flag.String("e", "err", "Hostnames value on error")
format = flag.String("f", "%v\n\t%v\n", "Format output (ip, hostnames)")
fileName = flag.String("i", "./addresses.txt", "Input file name")
sep = flag.String("s", "\n\t", "Hostnames separator")
trimDot = flag.Bool("t", true, "Trim hostnames \".\" suffix")
)
func loadLines(lines *[]string) {
file, err := os.Open(*fileName)
if err != nil {
panic(fmt.Sprintf("Error opening file \"%v\"", *fileName))
}
defer file.Close()
scanner := bufio.NewScanner(file)
for scanner.Scan() {
*lines = append(*lines, scanner.Text())
}
}
func lookup(ip string, c chan string) {
names, err := net.LookupAddr(ip)
if err != nil {
c <- fmt.Sprintf(*format, ip, *errVal)
return
}
if *trimDot {
for i := range names {
names[i] = strings.TrimSuffix(names[i], ".")
}
}
c <- fmt.Sprintf(*format, ip, strings.Join(names, *sep))
}
func main() {
flag.Parse()
var ipAddrs []string
loadLines(&ipAddrs)
c := make(chan string, len(ipAddrs))
for _, ip := range ipAddrs {
go lookup(ip, c)
}
for range ipAddrs {
fmt.Println(<-c)
}
}