查询ARP缓存以获取MAC ID [英] Query ARP cache to get MAC ID

查看:203
本文介绍了查询ARP缓存以获取MAC ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在网络中获取主机的MAC ID。为此,如果我ping到该IP并查询ARP缓存 arp -a ,我就能获得MAC ID。我只是想知道我是否可以获取任何API来查询ARP并获得MAC ID。

I need to get the MAC ID of a host in my network. For that, if I ping to that IP and query the ARP cache arp -a, I am able to get the MAC ID. I just wonder if I can get any API to query the ARP and get the MAC id.

此外,如果有更好的方法从IP地址获取MAC ID ,请建议。

Also, if there is a better method to get the MAC ID from IP address, please suggest.

PS:我在JAVA工作。

P.S: I am working in JAVA.

谢谢。

推荐答案

Java没有提供直接查询网络中主机MAC地址的方法,因为它被Java的套接字库抽象出来。

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.

在某种程度上,这是有道理的,因为主机的MAC地址实际上说的很少。没有主机的MAC地址。

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.


  • 许多主机都有几个NIC,都有一个单独的MAC地址,他们可以连接到网络。我现在使用的计算机有一个有线以太网适配器,一个WiFi适配器和一个Firewire适配器,它们都有自己的MAC地址。这意味着主机没有明确的MAC地址。

  • 如果主机位于不同的子网上,ARP实际上会为您提供数据包通过的最后一个路由器的MAC地址,而不是您正在扫描的主机的MAC地址。

将这两个问题放在一起,这意味着一个主机可能有许多不同的MAC地址(如果它有多个NIC),一个MAC地址可能代表许多不同的主机(如果流量通过路由器)。

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).

假设你知道这一切,你仍然需要得到一个主机的MAC地址,在Java中唯一的方法是去原生:

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":


  • 运行程序的客户端原生:


    • 您可以启动ARP命令行工具并解析其输出。

    • 您可以使用某种JNI调用。不过我对JNI并不太熟悉,所以我无法帮助你。

    • 编写一个单独的小型本机应用程序,您可以通过Telnet或某些此类方式从Java访问协议,它将为您运行ARP命令。


    • 您可以使用SNMP,因为该线程的其他一些答案建议。我将这些答案推迟给你。 SNMP是一个很好的协议,但要注意SNMP的OID可以依赖于平台,也可以依赖于供应商。适用于Windows的OID并不总是适用于Linux,反之亦然。

    • 如果您知道主机运行Windows,则可以使用 WMI Win32_NetworkAdapter 类保存您想要的信息,但请注意,这会返回主机NIC的所有,即使是Windows组成的。此外,它需要您正在扫描的主机的管理员凭据。 Google将告诉您如何从Java连接到WMI。

    • 如果您知道您的主机运行OS X,您可能可以通过SSH连接到计算机并解析<$ c的输出$ c> system_profile 命令。

    • 对于Linux,可能存在类似于OS X的 system_profile 的工具。

    • You could use SNMP, as some of the other answers to this thread suggest. I defer to these answers for making that work for you. SNMP is a great protocol, but be aware that SNMP's OIDs can be both platform-dependent and vendor-dependent. OIDs that work for Windows don't always work for Linux and vice versa.
    • If you know that your host runs Windows, you could use WMI. The Win32_NetworkAdapter class holds the information you want, but be aware that this returns all of the hosts NICs, even the ones Windows makes up. Also, it requires administrator credentials to the host you are scanning. Google will tell you how to connect to WMI from Java.
    • If you know your host runs OS X, you might be able to SSH into the machine and parse the output of the system_profile command.
    • For Linux, a tool similar to OS X's system_profile probably exists.

    这篇关于查询ARP缓存以获取MAC ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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