如何在Java中从客户端获取uuid或mac地址?
我正在寻找一种基于Java的Web应用程序的解决方案,以唯一地标识客户机。服务器与客户端位于同一网络中,我认为使用MAC地址将是一个很好的解决方案。 问题是我不能使用cookie,因为它们可以在客户端删除,我不能使用IP,因为它们可以发布新的DHCP租约续订 所以我想回到客户端的MAC地址。我知道没有java内置功能来获取MAC地址。 是否有一个库可以处理每个操作系统的输出?(主Windows和Mac),因为我的java应用程序在这两种平台上运行 或者,对于在网站和HTTP协议中唯一标识客户机,还有其他建议吗?(可能是HTML5数据存储或其他) 顺便说一句,我正在使用Java1.7如何在Java中从客户端获取uuid或mac地址?,java,web-applications,uuid,mac-address,Java,Web Applications,Uuid,Mac Address,我正在寻找一种基于Java的Web应用程序的解决方案,以唯一地标识客户机。服务器与客户端位于同一网络中,我认为使用MAC地址将是一个很好的解决方案。 问题是我不能使用cookie,因为它们可以在客户端删除,我不能使用IP,因为它们可以发布新的DHCP租约续订 所以我想回到客户端的MAC地址。我知道没有java内置功能来获取MAC地址。 是否有一个库可以处理每个操作系统的输出?(主Windows和Mac),因为我的java应用程序在这两种平台上运行 或者,对于在网站和HTTP协议中唯一标识客户机,
我不会强迫用户登录或以其他方式识别自己,也不会为客户端智能手机编写本机应用程序。我能找到的最好方法是: 总结如下:
- 没有标准的Java API
- 没有独立于操作系统的解决方案
- 您的应用程序通常需要具有特权(例如根访问权限)才能查询主机的ARP缓存,以及
- 如果数据包通过网络路由器,您将无法再识别源MAC地址
- 这只识别机器,而不是用户。某些计算机由多个用户共享
- MAC地址也可以更改
- 我写了自己的方法来解决我的问题。如果有人需要代码在同一网络中查找MAC地址,则可以使用此功能。在Win 7和Mac OS X 10.8.2上对我没有任何管理员权限
Pattern macpt = null;
private String getMac(String ip) {
// Find OS and set command according to OS
String OS = System.getProperty("os.name").toLowerCase();
String[] cmd;
if (OS.contains("win")) {
// Windows
macpt = Pattern
.compile("[0-9a-f]+-[0-9a-f]+-[0-9a-f]+-[0-9a-f]+-[0-9a-f]+-[0-9a-f]+");
String[] a = { "arp", "-a", ip };
cmd = a;
} else {
// Mac OS X, Linux
macpt = Pattern
.compile("[0-9a-f]+:[0-9a-f]+:[0-9a-f]+:[0-9a-f]+:[0-9a-f]+:[0-9a-f]+");
String[] a = { "arp", ip };
cmd = a;
}
try {
// Run command
Process p = Runtime.getRuntime().exec(cmd);
p.waitFor();
// read output with BufferedReader
BufferedReader reader = new BufferedReader(new InputStreamReader(
p.getInputStream()));
String line = reader.readLine();
// Loop trough lines
while (line != null) {
Matcher m = macpt.matcher(line);
// when Matcher finds a Line then return it as result
if (m.find()) {
System.out.println("Found");
System.out.println("MAC: " + m.group(0));
return m.group(0);
}
line = reader.readLine();
}
} catch (IOException e1) {
e1.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
// Return empty string if no MAC is found
return "";
}
IP地址的使用在本地网络中不起作用。我使用了其他一些方法来解析有用的命令,以获得MAC地址sysout
public String getMacAddress() throws Exception {
String macAddress = null;
String command = "ifconfig";
String osName = System.getProperty("os.name");
System.out.println("Operating System is " + osName);
if (osName.startsWith("Windows")) {
command = "ipconfig /all";
} else if (osName.startsWith("Linux") || osName.startsWith("Mac") || osName.startsWith("HP-UX")
|| osName.startsWith("NeXTStep") || osName.startsWith("Solaris") || osName.startsWith("SunOS")
|| osName.startsWith("FreeBSD") || osName.startsWith("NetBSD")) {
command = "ifconfig -a";
} else if (osName.startsWith("OpenBSD")) {
command = "netstat -in";
} else if (osName.startsWith("IRIX") || osName.startsWith("AIX") || osName.startsWith("Tru64")) {
command = "netstat -ia";
} else if (osName.startsWith("Caldera") || osName.startsWith("UnixWare") || osName.startsWith("OpenUNIX")) {
command = "ndstat";
} else {// Note: Unsupported system.
throw new Exception("The current operating system '" + osName + "' is not supported.");
}
Process pid = Runtime.getRuntime().exec(command);
BufferedReader in = new BufferedReader(new InputStreamReader(pid.getInputStream()));
Pattern p = Pattern.compile("([\\w]{1,2}(-|:)){5}[\\w]{1,2}");
while (true) {
String line = in.readLine();
System.out.println("line " + line);
if (line == null)
break;
Matcher m = p.matcher(line);
if (m.find()) {
macAddress = m.group();
break;
}
}
in.close();
return macAddress;
}
这应该适用于所有地方。至少,在Ubuntu机器上使用此方法会产生以下结果:
Operating System is Linux
line eth0 Link encap:Ethernet HWaddr f4:6d:04:63:8e:21
mac: f4:6d:04:63:8e:21
下面是答案:“如何在Java中获得唯一的计算机标识符(如磁盘id或主板id)”()我正在寻找仅使用Web服务的客户端的uuid。(不涉及客户端上的Java小程序),您的链接只涉及本地计算机。有关客户端的详细信息。它们都是智能手机,不能在多个用户之间共享。更改智能手机的MAC地址比删除Cookie要难得多。正如问题中所述,所有客户端都在同一个网络中。请重新阅读我的问题,我需要远程计算机的MAC。。。不是我自己的。这是不能保证的,因为“要能够ARP设备,您必须在该网络上的交换机上有一个接口(SVI)。要ARP设备,您必须在同一个第2层vlan上有一个与第3层IP的接口。”另一种选择是使用“getmac/s 00.00.00”其中00.00.00.00是远程ipaddress@Nexus2K我使用了你的代码,它在windows系统中正常工作,但在linux系统中不工作why@KVK它在LinuxMint下运行。如果您从
127.0.0.1
请求,它将找不到任何内容。实施它!