自动检测连接到网络的新计算机 [英] Automatically detect a new computer connected to the network

查看:49
本文介绍了自动检测连接到网络的新计算机的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有一种优雅的方法可以让程序检测连接到网络的新计算机?

Is there an elegant way to make a program detect a new computer that is connected to the network?

我希望我的程序能够自动感知"连接到网络上的新计算机(它们在同一网络上).就像 USB 设备连接到计算机一样.

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.

我现在正在做的是不时保存网络中所有计算机的列表.另一种方法是 PING 子网上所有可用的 IP.

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?

谢谢!

推荐答案

侦听 ARP 请求是执行此操作的规范方法.无论是否独立于 DHCP,任何希望与外界通信的连接计算机都必须向默认路由器的地址发出 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.

如果另一台计算机使用 DHCP,它会向自己的地址发出 ARP 请求,作为重复地址检测的一部分,这也是您可以监听的广播.

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.

(这与 IPv6 的工作方式大致相同,只是您需要查找邻居发现或路由器请求数据包.)

(This works more or less the same way for IPv6, except you need to look for neighbor discovery or router soliciation packets instead.)

就像所提到的答案,如果您有一个可以远程登录或使用 SNMP 的交换机,您可以提取 MAC 表.这将为您提供交换机中每个端口上的 MAC 地址列表.但是,如果您想要 IP 地址,您仍然需要侦听 ARP:s.

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.

另一方面,如果您可以访问网络上的默认网关,您还可以查看那里的 ARP 表.这将为您提供最近(对于最近的不同值...)与之通信的任何人的 MAC 和 IP 地址.

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.

这篇关于自动检测连接到网络的新计算机的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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