func main() {
var ip string
fmt.Print("Host: ")
fmt.Scanln(&ip)
addr, _ := net.LookupAddr(ip)
app := "grep"
arg0 := "inst"
arg1 := "acl.yaml"
cmd := exec.Command(app, arg0, arg1)
stdout, err := cmd.Output()
x := string(stdout)
reg, _ := regexp.Compile(`(internal)`)
if err != nil {
println(err.Error())
return
}
if ip != "" {
validateIP(ip)
}
fmt.Println(x)
fmt.Println(addr[0])
//fmt.Println(stdout)
if addr[0] == reg.FindString(`(internal)`){
file, err := os.Open("ports.json")
if err != nil {
panic(err)
}
defer file.Close()
ports := []portDef{}
err = json.NewDecoder(file).Decode(&ports)
if err != nil {
panic(err)
}
for _, p := range ports {
conn, err := net.Dial(p.Protocol, ip + ":" + p.Port)
if conn != nil {
fmt.Println("")
fmt.Printf("Connection established between %s and Localhost.
", addr)
fmt.Printf("Remote Address : %s
", conn.RemoteAddr().String())
fmt.Printf("Local Address : %s
", conn.LocalAddr().String())
}
fmt.Println("")
if err != nil {
fmt.Println(err)
}
}
}
}
I am trying to compare a regexp value to a string. In this case I want the value of "addr"
to be compared to the regex and if the value of addr
has "internal" in it that it opens the selected file and iterates through the ports. The address returned from the LookupAddr
is always in box.internal.blah.blah.com, so I am looking for the second field in the address (internal in this case) to be compared to the regex and if it finds "internal" in the address it will cycle through the loop.
I know my regexp is most likely what is wrong here. Any help would be greatly appreciated.