以独立于平台的方式从 IP 地址查找 Mac 地址

发布于 2025-01-03 22:00:38 字数 158 浏览 3 评论 0原文

我需要找出我的设备从中获取 TCP 请求的设备的 mac 地址,我将通过 tcp 端点获取设备的 IP 地址,但我需要找出设备的 mac 地址。我的应用程序将运行在Windows 和 Linux 都可以,所以请建议我一种跨平台的方法来查找 mac 地址。任何 boost 库都可以帮助我做同样的事情吗?

I need to findout the mac address of the device from which my device gets TCP requests, I ll be getting the ip address of the device by tcp endpoint but i need to find out the mac address of the device.My application will be running on both windows and linux, so please suggest me a cross platform method to find the mac address.. Any boost libraries will help me doing the same??

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

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

发布评论

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

评论(3

绿萝 2025-01-10 22:00:38

首先,您无法找到不在同一局域网上的任何网络接口的 MAC 地址。该信息不会传输到路由器之外。

有一个名为 arp 的命令行工具,可在 Unix 和 Windows 上使用,它会列出与您的 PC 通信的接口的 IP 地址和 MAC 地址。 ie

arp -a

在 Windows 上给出类似:

Interface: 9.175.198.236 --- 0x2
  Internet Address      Physical Address      Type
  9.175.198.129         00-1b-53-46-fa-7f     dynamic

在类似 Unix 上看起来像:

foo.bar.com (10.27.68.72) at 00:50:56:AE:00:0B [ether] on eth0
baz.bar.com (10.27.68.77) at 00:50:56:AE:00:10 [ether] on eth0
? (10.27.68.1) at 00:50:5A:1B:44:01 [ether] on eth0

您可以尝试调用它并以编程方式解析输出。

Firstly, you can't find the MAC address for any network interface that is not on the same local area network. That information is not transmitted beyond the router.

There is a command line tool called arp that is available on Unix and also Windows that will list IP addresses and MAC addresses of interfaces that have been in communication with your PC. i.e.

arp -a

on Windows gives something like:

Interface: 9.175.198.236 --- 0x2
  Internet Address      Physical Address      Type
  9.175.198.129         00-1b-53-46-fa-7f     dynamic

and on a Unix-alike looks like:

foo.bar.com (10.27.68.72) at 00:50:56:AE:00:0B [ether] on eth0
baz.bar.com (10.27.68.77) at 00:50:56:AE:00:10 [ether] on eth0
? (10.27.68.1) at 00:50:5A:1B:44:01 [ether] on eth0

You can try invoking it and parsing the output programmatically.

叫嚣ゝ 2025-01-10 22:00:38

arp 源代码可以在下面的链接中找到,选择您感兴趣的代码!这是c代码,所以应该可以正常工作。

http://www.opensource.apple.com /source/network_cmds/network_cmds-328/arp.tproj/arp.c

arp source code is available in the below link, take the piece of code that interests you! It is c code so it should work fine.

http://www.opensource.apple.com/source/network_cmds/network_cmds-328/arp.tproj/arp.c

深海里的那抹蓝 2025-01-10 22:00:38

首先要注意的是,在 TCP 层,您不知道 MAC 地址。

对于你的情况,我想你可以做两件事:

  1. 使用arp或编写一段类似于arp的代码,它在给定IP地址的情况下查找MAC。
    这种方法的问题是,当源位于另一个网络时,它不起作用。

  2. 以这样的方式编写服务器,使其从发送 TCP 请求的客户端请求此信息。这可以在 TCP 建立后完成。客户端还应该能够查找给定 IP 的计算机 MAC 地址。

First thing to note is that at TCP layer, you don't know the MAC addresses.

For your case, I guess you can do two things:

  1. use arp or write a piece of code similar to arp which looks for MAC given the IP address.
    The problem with this approach is that it won't work in cases when the source is in another network.

  2. write your server in such a way that it requests for this information from the client sending TCP request. This can be done post TCP establishment. The client should also be able to look up the machine's MAC address for the given IP.

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