查询ARP缓存以获取MAC ID
我需要获取网络中主机的 MAC ID。 为此,如果我 ping 到该 IP 并查询 ARP 缓存 arp -a
,我就能够获取 MAC ID。 我只是想知道是否可以获得任何 API 来查询 ARP 并获取 MAC id。
另外,如果有更好的方法从IP地址获取MAC ID,请建议。
PS:我是用JAVA工作的。
谢谢。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
有一种更简单的方法:
您将获得整个 ARP 表,其中每行都排序了 IP 和 HW。
然后,您可以将表拆分为单独的字符串行,并在每行上使用正则表达式来匹配硬件和 IP 地址。 你就完成了。
There is a much simpler way:
And you get the eintire ARP Table with IP and HW sorted on each row.
Then you can split the table into separate String rows and use regular expressions on each row to match both HW and IP Adress. And you're done.
正如其他人所说,ARP 是可行的方法。 以下是基于 GitSpot 上的这个示例。
需要两个库:
可从 jpcap sourceforge 站点,它通过 JNI 提供第一个库的高级接口
As others have said, ARP is the way to go. Following is an implementation of jqnos second suggestion based on this example on GitSpot.
Two libraries are required:
the jpcap java library available from the jpcap sourceforge site, which provides a high-level interface to the first library through JNI
这在 Java 环境中可能无法解决(因为它与平台无关),但您还应该考虑是否可以通过系统服务获取 MAC 地址。 在某些情况下,您可能无法通过 ARP 可靠地找到 MAC 地址,这取决于您需要 MAC 地址的原因。
This may not be solvable in the context of Java (because it is platform independent), but you should also consider whether or not you can get the MAC addresses via a system service. There are probably situations where you cannot reliably find the MAC address via ARP, it depends on why you would need the MAC address.
arp 缓存作为可用 SNMP 数据集中的标准提供。 您可以使用 SNMP4J 编写一个简单的代理来查询此数据。
例如,从命令行 SNMP 工具集
(那个巨大的句点分隔字符串是 SNMP 术语中 ARP 缓存的 OID 或标识符。这适用于所有 SNMP 实现)
The arp cache is provided as standard in the set of SNMP data available. You can use SNMP4J to write a trivial agent to query this data.
e.g. from a command line SNMP toolset
(that huge period-delimited string is the OID, or identifier, of the ARP cache in SNMP terms. That will work for all SNMP implementations)
ARP 是将 IP 地址映射到 MAC 地址的方式。 IP 堆栈就是这样做的。
我不确定是否有一种可移植的方式来获取该信息,因为它通常只对内核开发人员和系统管理员重要。
从大量的网络搜索来看,似乎可以使用 SNMP 获取路由器的 ARP 表,但我没有找到很多关于如何做到这一点的具体信息。 不过,我确实在这里找到了一个免费的 SNMP Java 库。 在那里进行一些洞穴探险可能会很有成效。
ARP is the way to map IP addresses to MAC addresses. That's how the IP stack does it.
I'm not sure there is a portable way to get that info, since it is typically only important for kernel developers and system administrators.
From a lot of web searching, it looks like it is possible to get a router's ARP table using SNMP, but I didn't find a lot of specific info on how to do it. I did find a free Java library for SNMP here though. Some spelunking through there might prove productive.
您可以通过以下方式获取您自己的 MAC 地址:
您绝对无法通过 vanilla java 获取另一台主机的 MAC 地址。 您必须使用进程执行或本机库来执行此操作。
如果你控制其他机器,你可以让他们查询自己的 MAC 并通过 TCP/IP 通道将其发送回,但我猜这不是你想要的。 更详细的可以看jqno的回答。
You can get your own MAC address via:
There is definitely no way you can get the MAC address of another host via vanilla java. You'd have to use Process execution or a native library to do it.
If you control the other machines, you can let them query their own MAC and send it back across a TCP/IP channel, but I'm guessing that's not what you want. For more details, see jqno's answer.
我在这里提供了一个完全生产就绪的方法,使用 pcap4j+libpcap 来检测 IPV4 和 ipv6 mac 地址: https://github.com/gaoxingliang/mac-address- detector-java
I provided a fully production ready method by using pcap4j+libpcap to detect IPV4 and ipv6 mac address here: https://github.com/gaoxingliang/mac-address-detector-java
受greenspand答案的启发,我想出了这段代码,它将使用IP查询MAC地址,并使用指定IP查询CMD命令。
请注意,此代码可以在 Windows 上运行,并且我相信它也可以在 Linux 上运行,只需稍加修改。
Inspired by greenspand answer i came up with this code that will query for the MAC address using IP and CMD command using specified IP.
Note that this code work on Windows and i believe it can work on Linux too with little modifications.
Java 没有提供直接的方法来查询网络中主机的 MAC 地址,因为这是由 Java 的套接字库抽象出来的。
在某种程度上,这是有道理的,因为主机的 MAC 地址实际上说明的很少。 不存在“主机”MAC 地址这样的东西。
将这两个问题放在一起,这意味着一台主机可能有许多不同的 MAC 地址(如果它有多个 NIC),并且一个 MAC 地址可能代表许多不同的主机(如果流量通过路由器)。
假设您知道所有这些,并且仍然需要获取主机的 MAC 地址,那么在 Java 中实现这一点的唯一方法是“本地化”:
system_profile
命令的输出。system_profile
的工具。Java provides no direct way to query the MAC address of a host in your network, as this is abstracted away by Java's socket libraries.
In a way, this makes sense, because the MAC address of a host actually says very little. There is no such thing as "the" MAC address of a host.
Put both of these issues together, and that means that one host may have many different MAC addresses (if it has more than one NIC), and one MAC address may represent many different hosts (if traffic passes through a router).
Assuming you know all this and you still need to get the MAC address of a host, the only way to do that in Java is by "going native":
system_profile
command.system_profile
probably exists.