-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
174 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,151 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"net" | ||
"os" | ||
"strings" | ||
"bufio" | ||
"fmt" | ||
"net" | ||
"os" | ||
"strings" | ||
"sync" | ||
) | ||
|
||
func main() { | ||
args := os.Args[1:] | ||
args := os.Args[1:] | ||
|
||
if len(args) != 1 { | ||
fmt.Println("Usage: ptr <ip_address>") | ||
return | ||
} | ||
if len(args) < 1 { | ||
printUsage() | ||
return | ||
} | ||
|
||
ip := args[0] | ||
arg := args[0] | ||
|
||
ptrDomainName, err := net.LookupAddr(ip) | ||
if err != nil { | ||
fmt.Println("No PTR record found for", ip) | ||
return | ||
} | ||
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\t%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 brackets from the PTR domain name. | ||
ptrDomainName = strings.Trim(ptrDomainName, "[") | ||
ptrDomainName = strings.Trim(ptrDomainName, "]") | ||
// Remove the last character if it is a dot. | ||
if ptrDomainName[len(ptrDomainName)-1] == '.' { | ||
ptrDomainName = ptrDomainName[:len(ptrDomainName)-1] | ||
} | ||
|
||
// Remove the last character if it is a dot. | ||
if ptrDomainName[len(ptrDomainName)-1] == '.' { | ||
ptrDomainName = ptrDomainName[:len(ptrDomainName)-1] | ||
} | ||
cleanedDomainNames = append(cleanedDomainNames, ptrDomainName) | ||
} | ||
|
||
fmt.Println(ptrDomainName) | ||
fmt.Printf("%s\t%s\n", ip, strings.Join(cleanedDomainNames, "\n")) | ||
} | ||
} |