Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get domain name from IP address in Go

Tags:

go

dns

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)
}
like image 583
Lander Avatar asked May 12 '13 22:05

Lander


People also ask

Can you find a domain name from an IP address?

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".

How do I convert an IP address to a domain name?

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.

How do I find hostname from IP address?

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).


2 Answers

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>
like image 184
peterSO Avatar answered Sep 20 '22 10:09

peterSO


You need LookupAddr instead of LookupHost.

like image 33
Ask Bjørn Hansen Avatar answered Sep 19 '22 10:09

Ask Bjørn Hansen