mac地址不可,出现多网卡,虚拟网卡;cpu序列号也不行同一批电脑配置相同返回的信息一样
CPU 序列号 配合 mac地址,两者同时使用,能唯一确定电脑吧
mac地址啊,用硬盘+网卡+主板,这三个就可以了
计算机ID一般是通过主板序列号配个其他参数确定的
CPU 序列号 配合 mac地址
第一,直接用IP判断,这种方式就是一个IP只能投票一定次数,方法可用下面代码中的getIpAddr() 中得到ipAddress的返回值判断即可。
第二,用MAC网卡标识进行判断,一台机器只能投票一定次数,方法可用下面代码中的getMAXAddress.但是此方法在很多网关设置了防火墙的情况下无法得到,而且还会造成异常警告,而且捕捉这个警告的时间还非常长,因此慎用。
第三,用ip地址和计算机名称联合判断,一个网段中计算机名称是不允许重名的,因此可用ip加计算机名称联合判断。方法可用下面代码中的getIpAddr() 的最后返回值即可
使用Java的bif能更准确找到
这个应该是板子底层串口驱动程序问题,丢数据了最后自己数据,可以用示波器看下原因
有wifi的情况下可以用MAC地址表示唯一,没wifi的情况下用时间字符串表示唯一
服务器的还是客户端的?服务器的可以用硬盘序列号,唯一
电脑的mac地址是唯一,如果是网卡是双网卡,直接 String command = "cmd.exe /c ipconfig /all";
Process p = Runtime.getRuntime().exec(command); 然后打印截取物理地址
/**
* 获取主板序列号
*
* @return
*/
public static String getMotherboardSN() {
String result = "";
try {
File file = File.createTempFile("realhowto", ".vbs");
file.deleteOnExit();
FileWriter fw = new java.io.FileWriter(file);
String vbs = "Set objWMIService = GetObject(\"winmgmts:\\\\.\\root\\cimv2\")\n"
+ "Set colItems = objWMIService.ExecQuery _ \n"
+ " (\"Select * from Win32_BaseBoard\") \n"
+ "For Each objItem in colItems \n"
+ " Wscript.Echo objItem.SerialNumber \n"
+ " exit for ' do the first cpu only! \n" + "Next \n";
fw.write(vbs);
fw.close();
Process p = Runtime.getRuntime().exec(
"cscript //NoLogo " + file.getPath());
BufferedReader input = new BufferedReader(new InputStreamReader(
p.getInputStream()));
String line;
while ((line = input.readLine()) != null) {
result += line;
}
input.close();
} catch (Exception e) {
e.printStackTrace();
}
return result.trim();
}
/**
* 获取硬盘序列号
*
* @param drive
* 盘符
* @return
*/
public static String getHardDiskSN(String drive) {
String result = "";
try {
File file = File.createTempFile("realhowto", ".vbs");
file.deleteOnExit();
FileWriter fw = new java.io.FileWriter(file);
String vbs = "Set objFSO = CreateObject(\"Scripting.FileSystemObject\")\n"
+ "Set colDrives = objFSO.Drives\n"
+ "Set objDrive = colDrives.item(\""
+ drive
+ "\")\n"
+ "Wscript.Echo objDrive.SerialNumber"; // see note
fw.write(vbs);
fw.close();
Process p = Runtime.getRuntime().exec(
"cscript //NoLogo " + file.getPath());
BufferedReader input = new BufferedReader(new InputStreamReader(
p.getInputStream()));
String line;
while ((line = input.readLine()) != null) {
result += line;
}
input.close();
} catch (Exception e) {
e.printStackTrace();
}
return result.trim();
}
/**
* 获取CPU序列号
*
* @return
*/
public static String getCPUSerial0() {
String result = "";
try {
Process process = Runtime.getRuntime().exec(
new String[] { "wmic", "cpu", "get", "ProcessorId" });
process.getOutputStream().close();
Scanner sc = new Scanner(process.getInputStream());
String property = sc.next();
result = sc.next();
} catch (Exception e) {
e.fillInStackTrace();
}
if (result.trim().length() < 1 || result == null) {
result = "无CPU_ID被读取";
}
return result.trim();
}
public static String getCPUSerial() {
String result = "";
try {
File file = File.createTempFile("tmp", ".vbs");
file.deleteOnExit();
FileWriter fw = new java.io.FileWriter(file);
String vbs = "Set objWMIService = GetObject(\"winmgmts:\\\\.\\root\\cimv2\")\n"
+ "Set colItems = objWMIService.ExecQuery _ \n"
+ " (\"Select * from Win32_Processor\") \n"
+ "For Each objItem in colItems \n"
+ " Wscript.Echo objItem.ProcessorId \n"
+ " exit for ' do the first cpu only! \n" + "Next \n";
// + " exit for \r\n" + "Next";
fw.write(vbs);
fw.close();
Process p = Runtime.getRuntime().exec(
"cscript //NoLogo " + file.getPath());
BufferedReader input = new BufferedReader(new InputStreamReader(
p.getInputStream()));
String line;
while ((line = input.readLine()) != null) {
result += line;
}
input.close();
file.delete();
} catch (Exception e) {
e.fillInStackTrace();
}
if (result.trim().length() < 1 || result == null) {
result = "无CPU_ID被读取";
}
return result.trim();
}
/**
* 获取MAC地址
*/
public static String getMac() {
String result = "";
try {
Process process = Runtime.getRuntime().exec("ipconfig /all");
InputStreamReader ir = new InputStreamReader(
process.getInputStream());
LineNumberReader input = new LineNumberReader(ir);
String line;
while ((line = input.readLine()) != null)
if (line.indexOf("Physical Address") > 0) {
String MACAddr = line.substring(line.indexOf(":") + 2);
result = MACAddr;
}
} catch (java.io.IOException e) {
System.err.println("IOException " + e.getMessage());
}
return result;
}
用ip地址和计算机名称联合判断,一个网段中计算机名称是不允许重名的,因此可用ip加计算机名称联合判断。方法可用下面代码中的getIpAddr() 的最后返回值即可
[java] view plain copy
/**
* 根据IP地址获取客户端的MAC
* @param ipAddress
* @return
* @throws IOException
*/
public static String getMACAddress(String ipAddress) throws IOException {
System.out.println("客户端的IP地址为:"+ipAddress);
String str = "", strMAC = "", macAddress = "";
Process pp = Runtime.getRuntime().exec("nbtstat -a " + ipAddress);
InputStreamReader ir = new InputStreamReader(pp.getInputStream());
LineNumberReader input = new LineNumberReader(ir);
for (int i = 1; i < 100; i++) {
str = input.readLine();
if (str != null) {
if (str.indexOf("MAC Address") > 1) {
strMAC = str.substring(str.indexOf("MAC Address") + 14,str.length());
break;
}
}
}
if (strMAC.length() < 17) {
return "Error!";
}
macAddress = strMAC.substring(0, 2) + ":" + strMAC.substring(3, 5)+ ":" + strMAC.substring(6, 8) + ":" + strMAC.substring(9, 11)+ ":" + strMAC.substring(12, 14) + ":"+ strMAC.substring(15, 17);
System.out.println("客户端的MAC地址为:"+macAddress);
return macAddress;
}
/**
* 获取客户端IP地址
* @return
*/
public static String getIpAddr() throws IOException{
HttpServletRequest req=ServletActionContext.getRequest();
String ipAddress = req.getHeader("x-forwarded-for");
if(ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress))
ipAddress = req.getHeader("Proxy-Client-IP");
if(ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress))
ipAddress = req.getHeader("WL-Proxy-Client-IP");
if(ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress))
ipAddress = req.getHeader("HTTP_X_FORWARDED_FOR");
if(ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress))
ipAddress = req.getHeader("HTTP_CLIENT_IP");
if(ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress))
ipAddress = req.getRemoteAddr();
String host = req.getRemoteHost();
String ip = ipAddress+":"+host;
System.out.println("访问者IP="+ip);
return ip;
}