如何检查两个主机是否相同
我刚刚遇到一个问题,我必须检查路径是否指向 Windows 共享。此问题的一部分是检查主机 A 是否与主机 B 相同。其中主机 A 和主机 B 可以是以下 {IPv4-Address、IPv6-Address、Hostname、FQDN} 之一。由于我不需要精确,因此在我的案例中解析和比较 IP 地址就足够了。
但理论上有没有一种方法可以检查主机是否相同?
I just came across an issue, where I had to check if a path points into a windows share. Part of this problem is to check if host A is the same as host B. Where host A and host B can be one of the following {IPv4-Address, IPv6-Address, Hostname, FQDN}. As I do not need to be exact it's enough to resolve and compare the IP-Addresses in my case.
But is there, theoretically, a method to check if the hosts are the same?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不,没有通用方法可以查看两个标识符(名称或地址)是否指向同一主机。一个示例:
192.0.2.1
和2001:db8::bad:dcaf
。它们是一样的吗? (答案:看不出来)互联网上没有通用的主机身份概念。有些协议有这样的概念,例如SSH(连接到主机,查看指纹是否match)或 HIP(测试密钥),但您依赖于为这些特定激活此协议机器。
一些启发式方法可能会有所帮助(例如,对机器进行指纹识别),但它会远未达到 100%。
No, there is no general method to see if two identifiers (names or addresses) go to the same host. One example:
192.0.2.1
and2001:db8::bad:dcaf
. Are they the same? (Answer: cannot tell)There is no general concept of host identity in the Internet. Some protocols have such a concept, for instance SSH (connect to the hosts, see if the fingerprints match) or HIP (test the keys) but you depend on this protocol being activated for these specific machines.
Some heuristics may help (fingerprinting the machine, for instance) but it will be far from 100 %.
将两个地址传递给
getaddrinfo()
函数,并比较结果列表是否有匹配项。与 gethostbyname() 不同,此函数将愉快地解析 IPv6 文字,并且还将返回与主机名关联的任何相关
A
或AAAA
记录。Pass the two addresses into the
getaddrinfo()
function, and compare the resulting lists for any matches.Unlike
gethostbyname()
, this function will happily parse IPv6 literals, and will also return any relevantA
orAAAA
records associated with a host name.