JAVA获取mac地址和cpu id(windows和linux)
public class HardwareUtil {
/**
* Return Opertaion System Name;
*
* @return os name.
*/
public static String getOsName() {
String os = "";
os = System.getProperty("os.name");
return os;
}
/**
* Returns the MAC address of the computer.
*
* @return the MAC address
*/
public static String getMACAddress() {
String address = "";
String os = getOsName();
if (os.startsWith("Windows")) {
try {
String command = "cmd.exe /c ipconfig /all";
Process p = Runtime.getRuntime().exec(command);
BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line;
while ((line = br.readLine()) != null) {
if (line.indexOf("Physical Address") > 0) {
int index = line.indexOf(":");
index += 2;
address = line.substring(index);
break;
}else if (line.indexOf("物理地址") > 0) {
int index = line.indexOf(":");
index += 2;
address = line.substring(index);
break;
}
}
br.close();
return address.trim();
} catch (IOException e) {
}
} else if (os.startsWith("Linux")) {
String command = "/bin/sh -c ifconfig -a";
Process p;
try {
p = Runtime.getRuntime().exec(command);
BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line;
while ((line = br.readLine()) != null) {
if (line.indexOf("HWaddr") > 0) {
int index = line.indexOf("HWaddr") + "HWaddr".length();
address = line.substring(index);
break;
}
}
br.close();
} catch (IOException e) {
}
}
address = address.trim();
return address;
}
/**
* 获取CPU号,多CPU时,只取第一个
* @return
*/
public static String getCPUSerial() {
String result = "";
String os = getOsName();
if (os.startsWith("Windows")) {
try {
File file = File.createTempFile("tmp", ".vbs");
file.deleteOnExit();
FileWriter fw = new java.io.FileWriter(file);
String vbs = "On Error Resume Next \r\n\r\n" + "strComputer = \".\" \r\n"
+ "Set objWMIService = GetObject(\"winmgmts:\" _ \r\n"
+ " & \"{impersonationLevel=impersonate}!\\\\\" & strComputer & \"\\root\\cimv2\") \r\n"
+ "Set colItems = objWMIService.ExecQuery(\"Select * from Win32_Processor\") \r\n "
+ "For Each objItem in colItems\r\n " + " Wscript.Echo objItem.ProcessorId \r\n "
+ " exit for ' do the first cpu only! \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();
}
}else if (os.startsWith("Linux")) {
String CPU_ID_CMD = "dmidecode -t 4 | grep ID |sort -u |awk -F': ' '{print $2}'";
Process p;
try {
p = Runtime.getRuntime().exec(new String[]{"sh","-c",CPU_ID_CMD});//管道
BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line;
while ((line = br.readLine()) != null) {
result += line;
break;
}
br.close();
} catch (IOException e) {
}
}
if (result.trim().length() < 1 || result == null) {
result = "无CPU_ID被读取";
}
return result.trim();
}
/**
* Main Class.
*
* @param args
* @throws NoSuchPaddingException
* @throws NoSuchAlgorithmException
*/
public static void main(String[] args) throws Exception {
String macAddress=HardwareUtil.getMACAddress();
String cpuSerial=HardwareUtil.getCPUSerial();
String test =macAddress+"@"+cpuSerial;
}
分享到:
相关推荐
前段时间摸索在Java中怎么获取系统信息包括cpu、内存、硬盘信息等,刚开始使用Java自带的包进行获取,但这样获取的内存信息不够准确并且容易出现找不到相应包等错误,所以后面使用sigar插件进行获取。下面列举出...
获取客户端ip地址( 这个必须从客户端传到后台): jsp页面下,很简单,request....因为系统的VIew层是用JSF来实现的,因此页面上没法直接获得类似request,在bean里做了个强制转换java 获取客户端ip mac地址
执行main方法获取mac地址,最权威的demo自己测试已经正常使用。网上基本都是假的获取不到,只有我这个才是最方便的
JAVA_MAC地址校验和转换,MAC地址递增,MAC地址加密
在对网内主机进行监控时需要通过java获取远程机器的mac地址
java获取mac,通过命令获取裁剪后得到想要的内容
用java获取CPU占用率 java 获取 CPU 占用率
获取用户请求IP地址 根据IP地址获取mac地址
java中通过ip获取远程客户端的mac地址,支持跨平台
java获取不同操作系统的名称以及mac地址工具类,例如:windows,Linux,Unix等
获取计算机的mac地址和ip地址,mac地址返回的是一个list,ip地址是string
得到机器的MAC,硬盘序列序列号和IP地址 public static void main(String[] args) ...{ String osName = System.getProperty("os.name"); StringBuffer systemPathBuff = new StringBuffer(""); if (osName.index...
主要介绍了java编程实现获取机器IP地址及MAC地址的方法,实例分析了Java分别针对单网卡及多网卡的情况下获取服务器IP地址与MAC地址的相关技巧,需要的朋友可以参考下
如何使用Java代码获取Android移动终端Mac地址.zip
Java获取系统信息(cpu,内存,硬盘,进程等)的相关方法.pdf
java 版的随机生成 MAC地址,笔者原创。 可根据自己需求定制。 /** * 根据虚拟机类型生成随机Mac地址 * * @param hypervType 虚拟机类型 * KVM: QEMU虚拟机 * vmware: Vmware虚拟机 * 其他: 生成随机的Mac...
用java实现获取MAC地址的方法,用java实现获取MAC地址的方法
Java获取本机MAC地址IP地址主机名
linux 下用java 获取系统信息 cpu mem jvm等 用java调用系统命令得到,方便实用