Java 如何从 HttpServlet 获取客户端的 MAC 地址?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/839973/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 20:04:47  来源:igfitidea点击:

how to get a client's MAC address from HttpServlet?

javatomcatservletstcp

提问by Amir Arad

I was asked to write a servlet that collects client's details such as ip, mac address etc.

我被要求编写一个 servlet 来收集客户端的详细信息,例如 ip、ma​​c 地址等。

getting his IP is pretty straight-forward (request.getRemoteAddr()) but I dont find an elegant way to get his MAC address.

获取他的 IP 非常简单(request.getRemoteAddr()),但我没有找到一种优雅的方式来获取他的 MAC 地址。

seems reasonable that the web server has access to data such as Mac address etc, since it gets the TCP packets and all. does this data registers somewhere? is it accessible?

Web 服务器可以访问诸如 Mac 地址等数据似乎是合理的,因为它获取了 TCP 数据包和所有内容。该数据是否在某处注册?可以访问吗?

(I'm working on top of Tomcat6)

(我正在 Tomcat6 之上工作)

采纳答案by atom255

You're probably not going to get what you want. (the client's MAC address)

你可能不会得到你想要的。(客户端的MAC地址)

If the server is close enough (directly connected via hub or maybe a switch) you can ARPfor the MAC Address. If you do this for an IP across the Internet you're probably going to get the inside interface of the closest Router or Switch.

如果服务器足够近(通过集线器或交换机直接连接),您可以为 MAC 地址进行ARP。如果您为 Internet 上的 IP 执行此操作,您可能会获得最近路由器或交换机的内部接口。

Because of the way TCP/IP works the MAC address used in the 'frame' will get ripped off and re-assembled each at each hop the information takes between the server and the host.

由于 TCP/IP 的工作方式,“帧”中使用的 MAC 地址将在服务器和主机之间获取的信息在每一跳中被撕掉并重新组装。

alt text

替代文字

Encapsulation

封装

回答by ivmos

TCP/IP... You can't get the MAC Address, that's a too low layer AFAIK

TCP/IP ... 你无法获得 MAC 地址,这是一个太低的层 AFAIK

回答by AndreiM

I believe that clients need to allow for this to happen in their JVM:

我相信客户需要允许这在他们的 JVM 中发生:

See this thread

看到这个线程

回答by Steve Claridge

This isn't possible through the HttpServlet class.

这通过 HttpServlet 类是不可能的。

The only way I can think of possibly gettnig a users MAC address is to use Javascript on the client side to retrieve it and then place it in a cookie that your server can then read. But, I don't know if it's possible to get MAC addr using Javascript - would seem like a security risk for a browser to allow you to do this as it's going outside the browser's sandbox but maybe there's some hack around to do it.

我能想到的可能获取用户 MAC 地址的唯一方法是在客户端使用 Javascript 来检索它,然后将其放入您的服务器可以读取的 cookie 中。但是,我不知道是否可以使用 Javascript 获取 MAC 地址 - 浏览器允许您这样做似乎是一种安全风险,因为它在浏览器的沙箱之外,但也许有一些黑客可以做到这一点。

回答by Akhil Sharma

this script works the best 100% probability that it works on you localhost but you have to check this with your webhost

此脚本最有可能 100% 地在您的本地主机上工作,但您必须与您的网络主机核对

there is a php code that is much more better

有一个更好的php代码

<font color="black" face="courier new">
<b>mac/linux/android</b>(arp -an)<br>

<?php
$mac = system('arp -an');
echo $mac;
echo "<hr>";
?>
<b>mac/linux/android</b>(ifconfig)<br>
<?php
$macall = system('ifconfig');
echo $macall;
echo "<hr>";
?>
<b>pc/win</b>(ipconfig /all)<br>
<?php
$pc = system('ipconfig /all');
echo $pc;
echo "<hr>";
?>
</font>