Created
July 31, 2018 19:08
-
-
Save eikaas/4486082ae30430780436e0b8e2419809 to your computer and use it in GitHub Desktop.
lookupipv6.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"net" | |
"os" | |
) | |
// LookupIPv6 looks up host using the local resolver. It returns a slice of that host's IPv6 addresses | |
func LookupIPv6(host string) ([]net.IP, error) { | |
addrs, err := net.LookupIP(host) | |
if err != nil { | |
return nil, err | |
} | |
var ipv6addrs []net.IP | |
for _, addr := range addrs { | |
if addr.To4() == nil { | |
ipv6addrs = append(ipv6addrs, addr) | |
} | |
} | |
return ipv6addrs, nil | |
} | |
func main() { | |
host := os.Args[1] | |
ips, err := LookupIPv6(host) | |
if err != nil { | |
fmt.Println(err) | |
os.Exit(1) | |
} | |
for _, ip := range ips { | |
fmt.Printf("IPv6: %s\n", ip.String()) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment