Linux 从 IP 地址获取主机名

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10748959/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 06:30:01  来源:igfitidea点击:

Get Hostname from IP Address

pythonlinuxnetwork-programming

提问by AjanShrestha

My problem is to match IP address to its hostname.

我的问题是将 IP 地址与其主机名匹配。

If the IP address is a public IP then a simple socket.gethostbyaddr(ip)solves it but my problem is with private IP i.e. IP of a network.

如果 IP 地址是公共 IP,那么一个简单的方法就socket.gethostbyaddr(ip)可以解决它,但我的问题是私有 IP,即网络的 IP。

How can I get host names from a network IP address? Is there a way to find out hostname or not?

如何从网络 IP 地址获取主机名?有没有办法找出主机名?

I'm trying to do this on Linux.

我正在尝试在 Linux 上执行此操作。

采纳答案by Mark

You've already got the Python code required

您已经获得了所需的 Python 代码

socket.gethostbyaddr(ip)

What you need is on the infrastructure side of things. To get an internal hostname, you need to query the internal DNS server for the network in question. Larger networks almost always have internal DNS services but some smaller network don't since they rely on other means (direct IP, NETBIOS, Bonjour, etc.) to find various resources.

您需要的是基础设施方面的东西。要获得内部主机名,您需要查询相关网络的内部 DNS 服务器。较大的网络几乎总是有内部 DNS 服务,但一些较小的网络没有,因为它们依赖其他方式(直接 IP、NETBIOS、Bonjour 等)来查找各种资源。

TL:DR: You need to query the internal DNS services for the network in question

TL:DR:您需要查询相关网络的内部 DNS 服务