Skip to content

Instantly share code, notes, and snippets.

@yehgdotnet

yehgdotnet/recordlookup.go

Last active Sep 26, 2020
Embed
What would you like to do?
recordlookup.go
package main
import (
"bufio"
"fmt"
"log"
"os"
"strings"
"regexp"
)
var (
path_ip = "ips.txt"
path_record = "records.txt"
)
func find_in_records(needle string,full_line string){
file, err := os.Open(path_record)
if err != nil {
log.Fatal(err)
}
defer file.Close()
scanner := bufio.NewScanner(file)
for scanner.Scan() {
line:= scanner.Text()
if strings.Contains(line,needle) {
result_line := line + full_line
result_line = strings.Replace(result_line,needle,"",2)
result_line = strings.Replace(result_line,"Ports:","",2)
fmt.Println(result_line + "\n\n")
break
}else{
//fmt.Println("not match: " + needle + " at line: " +line)
}
}
if err := scanner.Err(); err != nil {
log.Fatal(err)
}
}
func execute_find() {
file, err := os.Open(path_ip )
if err != nil {
log.Fatal(err)
}
defer file.Close()
scanner := bufio.NewScanner(file)
re := regexp.MustCompile(`(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)(\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3}`)
for scanner.Scan() {
line:= scanner.Text()
submatchall := re.FindAllString(line, -1)
if re.MatchString(line){
each_ip := submatchall[0]
find_in_records(each_ip,line)
}
}
if err := scanner.Err(); err != nil {
log.Fatal(err)
}
}
func main(){
execute_find()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment