Skip to content

Instantly share code, notes, and snippets.

@yosignals
Created January 2, 2023 13:01
Show Gist options
  • Save yosignals/3df1a3f519eaae282b5e97378cd9a8a2 to your computer and use it in GitHub Desktop.
Save yosignals/3df1a3f519eaae282b5e97378cd9a8a2 to your computer and use it in GitHub Desktop.
Grab IP4 and 6 Addresses from your logs and run local whois against them (recommending appending '| tee output.log '
package main
import (
"bufio"
"fmt"
"os"
"os/exec"
"regexp"
"sort"
)
func main() {
// Prompt the user for the path to the access log file
fmt.Println("Enter the path to the access log file:")
var filePath string
fmt.Scanln(&filePath)
// Read in the access log file
file, err := os.Open(filePath)
if err != nil {
fmt.Println(err)
return
}
defer file.Close()
// Extract the IP addresses from the file
ipRegex := regexp.MustCompile(`\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b`)
scanner := bufio.NewScanner(file)
var ipAddresses []string
for scanner.Scan() {
line := scanner.Text()
matches := ipRegex.FindAllString(line, -1)
ipAddresses = append(ipAddresses, matches...)
}
if err := scanner.Err(); err != nil {
fmt.Println(err)
return
}
// Sort and deduplicate the IP addresses
sort.Strings(ipAddresses)
deduplicated := make(map[string]bool)
for _, ip := range ipAddresses {
deduplicated[ip] = true
}
// Look up each IP address
for ip := range deduplicated {
fmt.Println("IP Address:", ip)
// Run the whois command
cmd := exec.Command("whois", ip)
output, err := cmd.CombinedOutput()
if err != nil {
fmt.Println(err)
continue
}
// Print the output of the whois command
fmt.Println(string(output))
fmt.Println()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment