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

查看:33
本文介绍了查询 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.

P.S:我在 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.

  • 许多主机将有多个网卡,所有网卡都有一个单独的 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 类包含您想要的信息,但请注意,这会返回所有主机网卡,甚至是 Windows 组成的网卡.此外,它还需要您扫描的主机的管理员凭据.Google 会告诉您如何从 Java 连接到 WMI.
    • 如果您知道您的主机运行的是 OS X,您或许可以通过 SSH 连接到机器并解析 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天全站免登陆