I'm trying to resolve the host name from an IP address, which is apparently proving to be a little more challenging than I thought it'd be.
I've tried using a couple of functions, including the net.LookupHost
method, but all of them seem to be just returning the IP address which I input.
Here's the code which I am using:
package main
import (
"fmt"
"net"
)
func main() {
// obtained from ping -c 1 stackoverflow.com, should print "stackoverflow.com"
addr, err := net.LookupHost("198.252.206.16")
fmt.Println(addr, err)
}
To find the domain name associated with an IP address, you can use the "dig" or "nslookup" command. For example, to find the domain name associated with the IP address "74.125. 224.72", you would use the command "dig -x 74.125. 224.72".
Open the tool: IP to Hostname Lookup. Enter any valid IP, and click on the "Convert IP to Hostname" button. The tool attempts to locate a DNS PTR record for that IP address and provides you the hostname to which this IP resolves.
This is another method to get the hostname from the IP address. Run the nslookup command with an IP address from which you want to get the hostname. This command works a bit differently from the ping command that is discussed above. See the syntax to run on command prompt (CMD).
For example,
package main
import (
"fmt"
"net"
)
func main() {
// obtained from ping -c 1 stackoverflow.com, should print "stackoverflow.com"
addr, err := net.LookupAddr("198.252.206.16")
fmt.Println(addr, err)
}
Output:
[stackoverflow.com.] <nil>
You need LookupAddr instead of LookupHost.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With