Mastering-Go-Second-Edition/ch04/findIPv4.go
Mihalis Tsoukalos c209f855aa Minor changes
2019-05-20 20:25:23 +03:00

53 lines
957 B
Go

package main
import (
"bufio"
"fmt"
"io"
"net"
"os"
"path/filepath"
"regexp"
)
func findIP(input string) string {
partIP := "(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])"
grammar := partIP + "\\." + partIP + "\\." + partIP + "\\." + partIP
matchMe := regexp.MustCompile(grammar)
return matchMe.FindString(input)
}
func main() {
arguments := os.Args
if len(arguments) < 2 {
fmt.Printf("usage: %s logFile\n", filepath.Base(arguments[0]))
os.Exit(1)
}
for _, filename := range arguments[1:] {
f, err := os.Open(filename)
if err != nil {
fmt.Printf("error opening file %s\n", err)
os.Exit(-1)
}
defer f.Close()
r := bufio.NewReader(f)
for {
line, err := r.ReadString('\n')
if err == io.EOF {
break
} else if err != nil {
fmt.Printf("error reading file %s", err)
break
}
ip := findIP(line)
trial := net.ParseIP(ip)
if trial.To4() == nil {
continue
}
fmt.Println(ip)
}
}
}