邻居发现C

发布于 2024-11-25 06:17:40 字数 155 浏览 3 评论 0原文

我需要发现Linux中的所有网络邻居(他们也运行Linux)并且我需要获取他们的IP地址(第三层)。有什么想法如何做到这一点吗?

顺便说一句,我需要在 C 中执行此操作,而不是在 shell 中,

提前非常感谢!

I need to discover all network neighbors in Linux(they are running Linux too) and I need to get theirs IP addresses(3rd layer). Any ideas how to do that?

Btw, I need to do that in C, not in shell

Many thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

诠释孤独 2024-12-02 06:17:40

您应该做的是,让邻居运行一个守护进程,该守护进程对 UDP 多播做出响应(向发送方发出单播响应)。

然后发送一个 TTL 为 1 的 UDP 多播(因此不会被路由)并监听谁响应。您只会收到来自运行代理的邻居的响应。

另一种可能性是使用已经执行此操作的现有协议,例如 mDNS。

What you should do is, have the neighbours run a daemon which responds (with a unicast response to the sender) to UDP multicasts.

Then send a UDP multicast with a TTL of 1 (so it will not be routed) and listen to see who responds. You will only receive responses from the neighbours which are running the agent.

Another possibility is to use an existing protocol which already does this, for example, mDNS.

躲猫猫 2024-12-02 06:17:40

如果相关机器不合作,则没有保证可以做到这一点。

您能做的最好的事情就是扫描可能的地址并探测每个地址,看看是否可以获得响应 - 该探测可以是任何内容,从简单的 ICMP 回显请求(ping)到试图引发响应的复杂的畸形数据包来自远程主机。

所需的复杂程度以及它是否能正常工作,完全取决于相关主机的防火墙程度等。

正如评论者已经观察到的那样,有像 nmap 这样的完整程序致力于尝试发现此信息,这让我们了解到这有多么重要。

在另一个极端,如果主机正在合作,那么简单的广播 ICMP 回显请求可能就足够了。

There is no guaranteed way to do this if the machines in question aren't co-operating.

The best you can do is to scan likely addresses and probe each one to see if you can get a response - that probe could be anything from a simple ICMP echo request (a ping) up to a sophisticated malformed packet that attempts to elicit a response from the remote host.

The level of sophistication required, and whether it will work at all, depends entirely on how heavily firewalled etc the host in question is.

As a commenter has already observed, there are entire programs like nmap dedicated to attempting to discover this information, which gives some idea of how non-trivial this can be.

At the other extreme, if the hosts are co-operating, then a simple broadcast ICMP echo request might be enough.

冰雪之触 2024-12-02 06:17:40

如果您的网段使用相当不错的交换机,您可以通过检查其中一台交换机的转发数据库来发现链路层邻居。您应该能够通过 SNMP 自动获取此信息,请检查交换机的文档。

获得链接邻居列表后,您可以尝试找出他们的 IP 地址,但请记住,他们可能有很多 IP 地址,也可能根本没有。为此,您需要某种反向 ARP。也许您的路由器维护着 MAC 到 IP 关联的列表,您可以查询它(SNMP 再次成为最方便的解决方案)。

If your segment uses reasonably decent switch, you can discover the link-layer neighbours by inspecting the forwarding database of one of the switches. You should be able to obtain this fairly automatically via SNMP, check your switch's documentation.

Once you have a list of link neighbours, you can try and find out their IP addresses, but remember that they may have many or none at all. For this you'd need some sort of reverse-ARP. Perhaps your router maintains a list of MAC-to-IP associations and you can query it (again SNMP would be the most convenient solution).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文