Skip to content

Instantly share code, notes, and snippets.

@yehgdotnet
Last active September 26, 2020 06:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yehgdotnet/1307019cb8af5e1e8ab6baae4e5f569b to your computer and use it in GitHub Desktop.
Save yehgdotnet/1307019cb8af5e1e8ab6baae4e5f569b to your computer and use it in GitHub Desktop.
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