简体   繁体   English

从 IP 地址获取 gTLD 或 ccTLD

[英]Get gTLD or ccTLD from IP address

There are many questions on SO related to fetching an IP address from URL, but not vice versa.关于从 URL 获取 IP 地址的 SO 有很多问题,但反之则不然。

As the title suggests, I would like to get the website URL of its respective IP address.正如标题所暗示的,我想获取其各自 IP 地址的网站 URL。 For instance:例如:

>>> import socket
>>> print(socket.gethostbyname('google.com'))

This looks up the domain and returns 172.217.20.14 .这会查找域并返回172.217.20.14 I am looking for the counter part like eg:我正在寻找计数器部分,例如:

>>> print(socket.getnamebyhost('172.217.20.14'))

Anything similar that would return the domain as google.com for the IP specified.任何类似的东西都会为指定的 IP 返回作为google.com的域。

Is this possible to do in python3?这可以在python3中做到吗? If yes, how can this be achieved?如果是,如何实现?


UPDATE更新

Unfortunately, the way I'm approaching this is wrong.不幸的是,我处理这个问题的方式是错误的。 There are IPs that share a one-to-many relationship ie the nameserver points to numerous urls, unless the PTR record indicates otherwise.有些 IP 共享一对多关系,即名称服务器指向多个 url,除非 PTR 记录另有说明。 My question rephrased:我的问题改写了:

How do IP-to-domain data providers like ipinfo.io return top-level domains for a single IP?ipinfo.io这样的 IP 到域数据提供程序如何返回单个 IP 的顶级域?

To my understanding, the A or AAAA records play an important role, but the only thing I get from these are ns rather than the domain.据我了解,A 或 AAAA 记录起着重要作用,但我从这些记录中获得的唯一信息是 ns 而不是域。 I don't know how to extract the gTLD or ccTLD from the records.我不知道如何从记录中提取 gTLD 或 ccTLD。 I'm open to any suggestions, if anyone is willing to share an answer on how to parse gTLD(s) or ccTLD(s) from any IP.如果有人愿意分享有关如何从任何 IP 解析 gTLD 或 ccTLD 的答案,我愿意接受任何建议。 Preferably in python, but a shell script would also suffice.最好在 python 中,但 shell 脚本也足够了。

The socket.gethostbyaddr('172.217.20.14'), would be the right way to go here, but not necessarily. socket.gethostbyaddr('172.217.20.14') 将是正确的方法,但不一定。 Here's why:原因如下:

Domain to IP resolution goes like:域到 IP 解析是这样的:

domain > root server > origin server > origin server's hostname to IP configurations.域 > 根服务器 > 源服务器 > 源服务器的主机名到 IP 配置。

Now to reverse engineer it, we have to take into account:现在要对其进行逆向工程,我们必须考虑:

  1. There can be multiple domains sharing that same IP address as is the case with shared hosting.可以有多个域共享相同的 IP 地址,就像共享主机一样。

  2. Assuming the domain has dedicated IP, the nslookup or gethostbyaddr 'should' return the domain name, but there can be proxy servers in-front, like Cloudflare and whatever Google is using.假设域有专用 IP,nslookup 或 gethostbyaddr “应该”返回域名,但前面可以有代理服务器,比如 Cloudflare 和 Google 使用的任何东西。

So even if you do this manually like try to find out actual IP google's server is running on you cannot, as that would open their central server for all kinds of attacks, most importantly DDoS.因此,即使您手动执行此操作,例如尝试找出运行在 Google 服务器上的实际 IP,您也不能这样做,因为这会打开其中央服务器以进行各种攻击,最重要的是 DDoS。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM