无法在/etc/hosts中设置本地网络上的远程计算机的主机名 [英] getting hostname of remote computers on the local network not setup in /etc/hosts

查看:87
本文介绍了无法在/etc/hosts中设置本地网络上的远程计算机的主机名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有了新的学习,我试图使用python的套接字获取主机名.

I have a new learning, I was trying to get hostname using python's socket.

因此在我的Macbook中,我运行了以下代码:

so from my macbook I ran the below code:

socket.gethostbyaddr("192.168.1.111")

,我得到('rock64',[],['192.168.1.111']),然后我尝试了不再位于网络上但曾经是的计算机的IP地址:

and I get the ('rock64', [], ['192.168.1.111']) then I tried IP address of a computer that is not on the network anymore but used to be:

socket.gethostbyaddr("192.168.1.189")

它返回:('mint',[],['192.168.1.189']),然后我意识到它来自/etc/hosts 文件.

and it returned: ('mint', [], ['192.168.1.189']) then I realised its coming from the /etc/hosts file.

现在在该主机文件中,我也有以下条目:

now in that host file I also have this entry:

/etc/hosts

172.217.25.3 google.com.hk

但是,如果我尝试从wan地址的IP获取主机,我得到的结果将与预期的不同!

but if I try to get host from ip of wan address i get different results than expected!

socket.gethostbyaddr("172.217.25.3")

socket.gethostbyaddr("172.217.25.3")

返回('hkg07s24-in-f3.1e100.net',['3.25.217.172.in-addr.arpa'],['172.217.25.3'])

所以我不奇怪在以后的WAN ip地址中要获取主机名,为什么在本地计算机IP的情况下要从配置的/etc/hosts文件获取主机名?

so I am not wondering where in the later case of WAN ip address I am getting the hostname and why in case of local computer IP's I am getting hostname from the configured /etc/hosts file ?

我们如何获得本地网络上主机的主机名,而无需 socket.gethostbyaddr 来查看/etc/hosts 文件或通过其他方式?>

How can we get hostname of host computers on the local network without socket.gethostbyaddr having to look into /etc/hosts file or by other means ?

推荐答案

这是对如何在本地网络上构建网络设备注册表?"这一问题的基于答案的答案.

This is opinion based answer to the question "how to build registry of network devices on your local network?"

在本地网络上建立设备注册表的最佳方法是在网关上设置 ntopng .它使用DPI(深度数据包检查)技术来收集有关主机的信息.NTOPNG具有漂亮的用户界面,并显示主机名(如果可能).您可以为不会通过任何协议泄漏主机名的特定主机分配别名.

The best way to build registry of devices on your local network is to setup ntopng on your gateway. It uses DPI (Deep Packet Inspection) Technics to collect information about hosts. NTOPNG has nice user interface and displays host names (when possible). You can assign aliases for specific hosts which do not leak host names via any protocol.

出于某些原因,ntopng开发人员未在请求的 http://您的服务器:3000/lua/host_get_json.lua?ifid = 2& host = IP-OF-DEVICE .

For some reasons ntopng developers did not include alias into json response for request http://YOUR-SERVER:3000/lua/host_get_json.lua?ifid=2&host=IP-OF-DEVICE .

您可以通过将以下行添加到文件/usr中来手动添加: require"mac_utils" hj ["alias"] = getDeviceName(hj ["mac_address"])/share/ntopng/scripts/lua/host_get_json.lua

You can add it manually by adding lines require "mac_utils" and hj["alias"]=getDeviceName(hj["mac_address"]) into file /usr/share/ntopng/scripts/lua/host_get_json.lua

您可以使用 REST API 来查询ntopng并使用提供的信息进行构建您需要的任何脚本.

You can use REST API to interrogate ntopng and use provided information for building any script you need.

这篇关于无法在/etc/hosts中设置本地网络上的远程计算机的主机名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆