自动检测连接到网络的新计算机
有没有一种优雅的方法可以让程序检测连接到网络的新计算机?
我希望我的程序能够“自动感知”网络上连接的新计算机(它们位于同一网络上)。就像 USB 设备连接到计算机一样。
我现在所做的就是不时保存网络中所有计算机的列表。另一种方法是 PING 子网上的所有可用 IP。
还有其他优雅的方法吗?
谢谢!
Is there an elegant way to make a program detect a new computer that is connected to the network?
I would like my program to "auto-sense" a new computer being connected on the network (they're on the same network). Like a USB device being connected to the computer.
What I'm doing now is to save a list of all computers in the network from time to time. Another approach is to PING all available IPs on the subnet.
Are there any other elegant approaches?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
侦听 ARP 请求是执行此操作的规范方法。无论是否独立于 DHCP,任何希望与外界通信的连接计算机都必须向默认路由器的地址发出 ARP 请求。该请求将以广播形式发出,并包含源接口的 MAC 和 IP 地址。
如果另一台计算机使用 DHCP,它会向自己的地址发出 ARP 请求,作为重复地址检测的一部分,这也是您可以窥探的广播。
(这与 IPv6 的工作方式大致相同,只是您需要寻找邻居发现或路由器请求数据包。)
就像提到的答案一样,如果您有一台可以 telnet 或使用 SNMP 的交换机,您可以提取 MAC 表。这将为您提供交换机中每个端口上的 MAC 地址列表。然而,如果您想要 IP 地址,您仍然需要侦听 ARP:s。
另一方面,如果您可以访问网络上的默认网关,您还可以查看那里的 ARP 表。这将为您提供最近与之通信的任何人的 MAC 和 IP 地址(对于最近的不同值...)。
Listening for ARP requests is the canonical way to do this. Independent of DHCP or not, any connected computer that wishes to communicate with the outside world will have to make an ARP request for the address of the default router. This request will go out as a broadcast, and contain the source interface's MAC and IP adresses.
If the other computer uses DHCP, it will make an ARP request for it's own address as part of duplicate address detection, which is also a broadcast you can snoop on.
(This works more or less the same way for IPv6, except you need to look for neighbor discovery or router soliciation packets instead.)
Like the answer alluded to, if you have a switch to which you can telnet or use SNMP on, you can extract the MAC table. That will give you a list of MAC adresses on each port in the switch. If you want the IP addresses however, you still need to listen for ARP:s.
On the other hand, if you have access to the default gateway on the network, you can also look at the ARP table there. That will give you MAC and IP addresses for anyone that has recently (for different values of recently...) communicated with it.
如果您有某种托管交换机,您可能可以连接到它,这将是一个相当优雅的方法。
如果您位于域中,则可以从域控制器获取加入该域的所有计算机的列表。
如果做不到这一点,我能想到的就是挑战/响应(例如 ping 它们)或检测从它们发送的流量(参见 这个问题也许可以作为一个起点?),这两种方法都没有让我觉得优雅。
If you have a managed switch of some kind, you could probably connect to that, that would be a fairly elegant method.
If you're on a domain, you can can get a list of all the machines joined to the domain from the domain controller.
Failing that, all I can think of is either a challenge/response thing (e.g. pinging them) or by detecting traffic sent from them (see this question maybe as a starting point?), neither of which strike me as an elegant approach.