如何在 C 中检索外部主机的 MAC 地址

发布于 2024-07-20 00:55:30 字数 293 浏览 12 评论 0原文

目前我们正在解析命令行的 arp 请求输出。

string cmd = "arp -n ";
cmd.append(ipaddress);
cmd.append(" | grep ");
cmd.append(ipaddress);
fgets( line, 130, fp);
fgets( line, 130, fp);
ret.append(line);
 ...

它可以工作,但是有没有一种方法可以使用不会过多依赖本机命令行界面的库函数来执行此操作? 该项目当前正在使用 libpcap。

Currently we're parsing arp request output from the command line.

string cmd = "arp -n ";
cmd.append(ipaddress);
cmd.append(" | grep ");
cmd.append(ipaddress);
fgets( line, 130, fp);
fgets( line, 130, fp);
ret.append(line);
 ...

It works, but is there a way to do this using a library function that wont depend so much on the native command line interface? The project is using libpcap currently.

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

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

发布评论

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

评论(2

ˉ厌 2024-07-27 00:55:31

我有点担心

获取外部主机的MAC地址

检索问题的

部分。 我希望您知道,您只能获取同一网络上计算机的 ARP 表中的条目。 如果您通过路由器连接到计算机,那么您将只能在 ARP 表中看到路由器的 MAC 地址。 因此,除非它是同一网络上的主机(不涉及路由器),否则无法知道外部主机的 MAC 地址

因此,答案是不可能通过路由器访问主机,并且如果在本地网络上可以访问主机,则只能使用特定于平台的访问方法,因为甚至 arp 命令的输出也可能因平台而异。

I am a little worried about the

retrieve foreign host’s MAC address

part of your question.

I hope that you are aware that you only get entries in the ARP table for machines on the same network. If you connect to a machine via a router then you will only see the routers MAC address in the ARP table. So there is no way of knowing the foreign host’s MAC address unless it's a host on the same network (no routers involved).

So the answer is that it is impossible for hosts that are reached via a router and in case the host is reachable on the local network you can use only platform specific access methods, as even the output of the arp command may differ between platforms.

◇流星雨 2024-07-27 00:55:31

一般来说,这取决于您的操作系统。 对此没有真正的标准 API。

假设您使用的是 Linux,打开并解析 /proc/net/arp。 其格式与 arp 命令的输出类似。 请注意,您最近必须至少向相关 IP 发送一次数据包,以便将其包含在 ARP 表中,当然,您不会获得本地网段之外的任何内容。

将 IP 放入此缓存非常简单 - 例如,只需在某个未使用的端口上向其发送 UDP 数据包即可 - 然后轮询直到它出现。

另一种选择是使用原始套接字来制作并发送您自己的 arp 数据包,但这要复杂得多:)如果您确实想走这条路,请研究 arping(页面今天已关闭,但git repo 已上线),这也可能通过使用像 libpcap 这样的平台无关的库来为您提供更多的可移植性(但需要root)

In general, this will depend on your OS. There's no real standard API for this.

Assuming you're on linux, open and parse /proc/net/arp. The format is similar to that of the output from the arp command. Note that you must send a packet to the IP in question at least once recently in order to have it in the ARP table, and of course you won't get anything outside of your local segment.

Getting an IP into this cache is easy enough - just send a UDP packet to it on some unused port, for example - then poll until it shows up.

Another alternative would be to use a raw socket to craft and send your own arp packet, but that's much more complex :) If you do want to go down this route, study the source code for arping (page is down today, but the git repo is online), which might also give you a bit more portability by using platform-independent-ish libraries like libpcap (but requires root)

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