I need to get primary domain name from ip. I have some doubts about how functions like gethostbyaddr and getfqdn work.
在以下示例中,我将反向IP随机域,然后尝试重新获得域名:
import socket
domain = 'heroku.com'
# get ip from domain
ip = socket.gethostbyname(domain)
print('ip =', ip)
# get domain from ip
print(socket.gethostbyaddr(ip))
print(socket.getfqdn(ip))
# OUTPUT
# ip = 50.19.85.154
# ('ec2-50-19-85-154.compute-1.amazonaws.com', ['154.85.19.50.in-addr.arpa'], ['50.19.85.154'])
# ec2-50-19-85-154.compute-1.amazonaws.com
It seems both gethostbyaddr
and getfqdn
are returning the public DNS of one of the load balanced ec2 on AWS. My question is why they don't return the domain heroku.com
which is probably the domain registered on Route53?
Another example with google.com
:
import socket
domain = 'google.com'
# get ip from domain
ip = socket.gethostbyname(domain)
print('ip =', ip)
# get domain from ip
print(socket.gethostbyaddr(ip))
print(socket.getfqdn(ip))
# OUTPUT
# ip = 216.58.208.174
# ('mil07s10-in-f14.1e100.net', ['174.208.58.216.in-addr.arpa', 'lhr25s09-in-f14.1e100.net', 'lhr25s09-in-f174.1e100.net'], ['216.58.208.174'])
# mil07s10-in-f14.1e100.net
Here again it seems they are returning the public DNS of some machine on GCP. How can I get the real primary domain name from an ip address (heroku.com
and google.com
in these examples)?
When we do a DNS lookup of a hostname, in the most of the cases we are returned with the
CNAME
(in some cases we may directly get IP). We take thatCNAME
, and further resolve it to get an IP. But multipleCNAME
's in the (n-1)th stage can be mapped to theCNAME
in the (n)th stage. Therefore getting back theCNAME
from theCNAME
of the later stages is a not a trivial task.另一种可能的方式
Well, now the discussion is moving away from the DNS, but I hope it may help you in achieving your task. Every router or node in the internet is mapped to a Autonomous System, and there are some organizations or sites which maintain this mapping database. So by having the IP, we can contact one such database to get its Autonomous System Number (ASN) and the organization to which the node belongs to.
whois.cymru.com:43
is one such site. You can use simple network client likenc
to query its database. Below I attached the screenshot of one such query.