Skip to content

Instantly share code, notes, and snippets.

@vadviktor
Created June 24, 2019 16:32
Show Gist options
  • Select an option

  • Save vadviktor/a7256dbaf262e1a108c2032d28a35aac to your computer and use it in GitHub Desktop.

Select an option

Save vadviktor/a7256dbaf262e1a108c2032d28a35aac to your computer and use it in GitHub Desktop.
Find IP address that matches a subset.
package main
import (
"bytes"
"fmt"
"log"
"net"
)
func main() {
ifaces, err := net.Interfaces()
if err != nil {
log.Print(fmt.Errorf("localAddresses: %v\n", err.Error()))
return
}
for _, i := range ifaces {
addrs, err := i.Addrs()
if err != nil {
log.Print(fmt.Errorf("localAddresses: %v\n", err.Error()))
continue
}
for _, a := range addrs {
switch v := a.(type) {
case *net.IPNet:
needle := []byte(`10.7.7.`)
hay := []byte(v.IP.String())
if bytes.Contains(hay, needle) {
log.Println("YES")
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment