简体   繁体   English

获取已连接网络设备的唯一 ID

[英]Get an unique id for a connected network device

I am connected to a WLAN, where a special hardware device is connected to as well.我连接到 WLAN,其中还连接了一个特殊的硬件设备。 I communicate to that device via a socket, since I know its IP.我通过套接字与该设备通信,因为我知道它的 IP。

Is there a was to identify that hardware device in the network by an id?是否可以通过 id 来识别网络中的硬件设备? I found out in Java it is not possible to obtain the MAC-address of a connected device.我发现在 Java 中无法获得连接设备的 MAC 地址。 Is there any other alternative?还有其他选择吗?

Thanks, best regards谢谢,最好的问候

Mac addresses should be unique. Mac 地址应该是唯一的。 Maybe you can get needed information from the ARP table.也许您可以从 ARP 表中获取所需的信息。 Command "arp -a" works on Windows and Linux.命令“arp -a”适用于 Windows 和 Linux。

But there is a problems:但是有一个问题:

  1. This is not portable way这不是便携方式
  2. The ARP table is quite variable ARP表变化很大
  3. If the device is behind a router, then this does not work.如果设备在路由器后面,则这不起作用。

In Java you can call NetworkInterface.getHardwareAddress() that will return hardware MAC address在 Java 中,您可以调用NetworkInterface.getHardwareAddress()返回硬件 MAC 地址

Enumeration<NetworkInterface> enumNicList = NetworkInterface.getNetworkInterfaces();
while(enumNicList.hasMoreElements())
{
    NetworkInterface oNic = enumNicList.nextElement();
    byte[] baMacAddress = oNic.getHardwareAddress();
    String sMacAddress = new BigInteger(1, baMacAddress).toString(16);
    System.out.println(sMacAddress);
}

If you don't have any control of the responses of the device, and the device doesn't contain any identifying API calls and such, then just use the IP address and make that IP statically assigned to that device via your router.如果您无法控制设备的响应,并且设备不包含任何识别 API 调用等,那么只需使用 IP 地址并通过路由器将该 IP 静态分配给该设备。 Then you can either create your own table of IP <-> device list, or even scrape the IP table off your router.然后,您可以创建自己的 IP <-> 设备列表表,甚至可以从路由器上删除 IP 表。

Come to think of it, you could probably get the MAC address the same way - scrape the DHCP table off your router's configuration screen.想想看,您可能会以相同的方式获取 MAC 地址 - 从路由器的配置屏幕上刮下 DHCP 表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM