September 2017
Intermediate to advanced
466 pages
9h 33m
English
The name of the Go utility that will return the hostname of an IP address will be lookIP.go and will be presented in three parts.
The first part is the following:
package main import ( "fmt" "net" "os" )
The second part has the following Go code:
func main() {
arguments := os.Args
if len(arguments) == 1 {
fmt.Println("Please provide an IP address!")
os.Exit(100)
}
IP := arguments[1]
addr := net.ParseIP(IP)
if addr == nil {
fmt.Println("Not a valid IP address!")
os.Exit(100)
}
The net.ParseIP() function allows you to verify the validity of the given IP address and is pretty handy for catching illegal IP addresses such as 288.8.8.8 and 8.288.8.8.
The last part of the utility is the following:
hosts, err := net.LookupAddr(IP) ...
Read now
Unlock full access