-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathptr.go
151 lines (122 loc) · 2.91 KB
/
ptr.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package main
import (
"bufio"
"fmt"
"net"
"os"
"strings"
"sync"
)
func main() {
args := os.Args[1:]
if len(args) < 1 {
printUsage()
return
}
arg := args[0]
if isFile(arg) {
processFile(arg)
} else if arg == "-" {
processStdin()
} else {
processSingleIP(arg)
}
}
func printUsage() {
fmt.Println("Usage:")
fmt.Println(" ptr <ip_address_or_input_file>")
fmt.Println()
fmt.Println("Options:")
fmt.Println(" <ip_address_or_input_file> Provide an IP address, an input file with one IP address per line, or use '-' to read IP addresses from stdin.")
}
func isFile(path string) bool {
info, err := os.Stat(path)
if err != nil {
return false
}
return !info.IsDir()
}
func processFile(filePath string) {
file, err := os.Open(filePath)
if err != nil {
fmt.Println("Error opening file:", err)
return
}
defer file.Close()
scanner := bufio.NewScanner(file)
var wg sync.WaitGroup
ipChannel := make(chan string)
// Start worker goroutines
for i := 0; i < 10; i++ { // Adjust the number of goroutines as needed
wg.Add(1)
go processIPs(ipChannel, &wg)
}
// Send IPs to worker goroutines through channel
for scanner.Scan() {
ip := strings.TrimSpace(scanner.Text())
if ip != "" {
ipChannel <- ip
}
}
close(ipChannel)
wg.Wait()
if err := scanner.Err(); err != nil {
fmt.Println("Error reading file:", err)
}
}
func processStdin() {
scanner := bufio.NewScanner(os.Stdin)
var wg sync.WaitGroup
ipChannel := make(chan string)
// Start worker goroutines
for i := 0; i < 10; i++ { // Adjust the number of goroutines as needed
wg.Add(1)
go processIPs(ipChannel, &wg)
}
// Send IPs to worker goroutines through channel
for scanner.Scan() {
ip := strings.TrimSpace(scanner.Text())
if ip != "" {
ipChannel <- ip
}
}
close(ipChannel)
wg.Wait()
if err := scanner.Err(); err != nil {
fmt.Println("Error reading input:", err)
}
}
func processSingleIP(ip string) {
var wg sync.WaitGroup
ipChannel := make(chan string)
// Start worker goroutines
for i := 0; i < 10; i++ { // Adjust the number of goroutines as needed
wg.Add(1)
go processIPs(ipChannel, &wg)
}
// Send the single IP to worker goroutines through channel
ipChannel <- ip
close(ipChannel)
wg.Wait()
}
func processIPs(ipChannel <-chan string, wg *sync.WaitGroup) {
defer wg.Done()
for ip := range ipChannel {
ptrDomainNames, err := net.LookupAddr(ip)
if err != nil {
fmt.Printf("%s,%v\n", ip, err)
continue
}
var cleanedDomainNames []string
for _, ptrDomainName := range ptrDomainNames {
// Remove the brackets from the PTR domain name.
ptrDomainName = strings.Trim(ptrDomainName, "[")
// Remove the last character if it is a dot.
if ptrDomainName[len(ptrDomainName)-1] == '.' {
ptrDomainName = ptrDomainName[:len(ptrDomainName)-1]
}
cleanedDomainNames = append(cleanedDomainNames, ptrDomainName)
}
fmt.Printf("%s,%s\n", ip, strings.Join(cleanedDomainNames, "\n"))
}
}