import java.io.File;
import java.io.FileWriter;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader; import com.tjhq.hqoa.utils.Log4jUtil;
import com.tjhq.hqoa.utils.StringUtil; //取主板序列号
public class MainBordUtil {
/**
* 获取当前操作系统名称. return 操作系统名称 例如:windows xp,linux 等.
*/
public static String getOSName() {
return System.getProperty("os.name").toLowerCase();
} public static String getMainBordId_windows() {
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) {
Log4jUtil.error("获取主板信息错误",e);
}
return result.trim();
} public static String getMainBordId_linux() { String result = "";
String maniBord_cmd = "dmidecode | grep 'Serial Number' | awk '{print $3}' | tail -1";
Process p;
try {
p = Runtime.getRuntime().exec(
new String[] { "sh", "-c", maniBord_cmd });// 管道
BufferedReader br = new BufferedReader(new InputStreamReader(
p.getInputStream()));
String line;
while ((line = br.readLine()) != null) {
result += line;
break;
}
br.close();
} catch (IOException e) {
Log4jUtil.error("获取主板信息错误",e);
}
return result;
} public static String getMainBordId() throws Exception {
String os = getOSName();
String mainBordId = "";
if (os.startsWith("windows")) {
mainBordId = getMainBordId_windows();
} else if (os.startsWith("linux")) {
mainBordId = getMainBordId_linux();
}
if(!StringUtil.isNotNullOrBlank(mainBordId)){
mainBordId="null";
}
return mainBordId;
} public static void main(String[] args) throws Exception {
String mainBord = getMainBordId();
System.out.println(mainBord);
}
}
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List; import com.tjhq.hqoa.utils.Log4jUtil;
import com.tjhq.hqoa.utils.StringUtil; /**
* 与系统相关的一些常用工具方法.
*
* @version 1.0.0
*/
public class MACUtil { /**
* 获取当前操作系统名称. return 操作系统名称 例如:windows xp,linux 等.
*/
public static String getOSName() {
return System.getProperty("os.name").toLowerCase();
} /**
* 获取unix网卡的mac地址. 非windows的系统默认调用本方法获取. 如果有特殊系统请继续扩充新的取mac地址方法.
*
* @return mac地址
*/
public static String getMAC_linux() {
String mac = null;
BufferedReader bufferedReader = null;
Process process = null;
try {
// linux下的命令,一般取eth0作为本地主网卡
process = Runtime.getRuntime().exec("ifconfig eth0");
// 显示信息中包含有mac地址信息
bufferedReader = new BufferedReader(new InputStreamReader(
process.getInputStream()));
String line = null;
int index = -1;
while ((line = bufferedReader.readLine()) != null) {
// 寻找标示字符串[hwaddr]
index = line.toLowerCase().indexOf("hwaddr");
if (index >= 0) {// 找到了
// 取出mac地址并去除2边空格
mac = line.substring(index + "hwaddr".length() + 1).trim();
break;
}
}
} catch (IOException e) {
Log4jUtil.error("获取mac信息错误",e);
} finally {
try {
if (bufferedReader != null) {
bufferedReader.close();
}
} catch (IOException e1) {
Log4jUtil.error("获取mac信息错误",e1);
}
bufferedReader = null;
process = null;
}
return mac;
} /**
* 获取widnows网卡的mac地址.
*
* @return mac地址
*/
public static String getMAC_windows() {
InetAddress ip = null;
NetworkInterface ni = null;
List<String> macList = new ArrayList<String>();
try {
Enumeration<NetworkInterface> netInterfaces = (Enumeration<NetworkInterface>) NetworkInterface
.getNetworkInterfaces();
while (netInterfaces.hasMoreElements()) {
ni = (NetworkInterface) netInterfaces.nextElement();
// ----------特定情况,可以考虑用ni.getName判断
// 遍历所有ip
Enumeration<InetAddress> ips = ni.getInetAddresses();
while (ips.hasMoreElements()) {
ip = (InetAddress) ips.nextElement();
if (!ip.isLoopbackAddress() // 非127.0.0.1
&& ip.getHostAddress().matches(
"(\\d{1,3}\\.){3}\\d{1,3}")) {
macList.add(getMacFromBytes(ni.getHardwareAddress()));
}
}
}
} catch (Exception e) {
Log4jUtil.error("获取mac错误", e);
}
if (macList.size() > 0) {
return macList.get(0);
} else {
return "";
} } private static String getMacFromBytes(byte[] bytes) {
StringBuffer mac = new StringBuffer();
byte currentByte;
boolean first = false;
for (byte b : bytes) {
if (first) {
mac.append("-");
}
currentByte = (byte) ((b & 240) >> 4);
mac.append(Integer.toHexString(currentByte));
currentByte = (byte) (b & 15);
mac.append(Integer.toHexString(currentByte));
first = true;
}
return mac.toString().toUpperCase();
} public static String getMAC() throws Exception {
String os = getOSName();
String mac = "";
if (os.startsWith("windows")) {
mac = getMAC_windows();
} else if (os.startsWith("linux")) {
mac = getMAC_linux();
}
if(!StringUtil.isNotNullOrBlank(mac)){
mac="null";
}
return mac;
} /**
* 测试用的main方法.
*
* @param argc
* 运行参数.
* @throws Exception
*/
public static void main(String[] argc) throws Exception {
String mac = getMAC();
System.out.println(mac);
}
}
import java.io.BufferedReader;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader; import com.tjhq.hqoa.utils.Log4jUtil;
import com.tjhq.hqoa.utils.StringUtil; public class CPUUtil {
/**
* 获取当前操作系统名称. return 操作系统名称 例如:windows xp,linux 等.
*/
public static String getOSName() {
return System.getProperty("os.name").toLowerCase();
} /**
* 获取CPU序列号
*
* @return
*/
public static String getCPUID_Windows() {
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) {
Log4jUtil.error("获取cpu信息错误",e);
}
return result.trim();
} public static String getCPUID_linux() throws InterruptedException {
String result = "";
String CPU_ID_CMD = "dmidecode";
BufferedReader bufferedReader = null;
Process p = null;
try {
p = Runtime.getRuntime().exec(new String[]{ "sh", "-c", CPU_ID_CMD });// 管道
bufferedReader = new BufferedReader(new InputStreamReader(
p.getInputStream()));
String line = null;
int index = -1;
while ((line = bufferedReader.readLine()) != null) {
// 寻找标示字符串[hwaddr]
index = line.toLowerCase().indexOf("uuid");
if (index >= 0) {// 找到了
// 取出mac地址并去除2边空格
result = line.substring(index + "uuid".length() + 1).trim();
break;
}
} } catch (IOException e) {
Log4jUtil.error("获取cpu信息错误",e);
}
return result.trim();
} public static String getCPUId() throws InterruptedException {
String os = getOSName();
String cpuId = "";
if (os.startsWith("windows")) {
cpuId = CPUUtil.getCPUID_Windows();
} else if (os.startsWith("linux")) {
cpuId = CPUUtil.getCPUID_linux();
}
if(!StringUtil.isNotNullOrBlank(cpuId)){
cpuId="null";
}
return cpuId;
} public static void main(String[] args) throws Exception {
String os = getOSName();
System.out.println(os);
String cpuid = getCPUId();
System.out.println(cpuid);
} }

Java获取主板序列号、MAC地址、CPU序列号工具类的更多相关文章

  1. Java获取本机MAC地址[转]

    原文地址:https://www.cnblogs.com/hxsyl/p/3422191.html Java获取本机MAC地址   为什么写这个呢?因为前几天看见网上有采用windows命令获取局域网 ...

  2. java获取本地计算机MAC地址

    java获取本地计算机MAC地址代码如下: public class SocketMac { //将读取的计算机MAC地址字节转化为字符串 public static String transByte ...

  3. Java获取本机MAC地址

    为什么写这个呢?因为前几天看见网上有采用windows命令获取局域网和广域网MAC,查了查可以直接用JDK的方法. MAC可用于局域网验证,提高安全性. import java.net.InetAdd ...

  4. Java获取网卡的mac地址

    为了项目的安全,有时候需要得到电脑的唯一码,比如:网卡的mac地址.和大家分享一下,下面是项目中用到的工具类: import java.io.BufferedReader;import java.io ...

  5. Java 获取年份的第一天或最后一天 工具类

    package com.taiping.test; import java.text.SimpleDateFormat; import java.util.Calendar; import java. ...

  6. c#中如何获取本机MAC地址、IP地址、硬盘ID、CPU序列号等系统信息

    我们在利用C#开发桌面程序(Winform)程序的时候,经常需要获取一些跟系统相关的信息,例如用户名.MAC地址.IP地址.硬盘ID.CPU序列号.系统名称.物理内存等. 首先需要引入命名空间: us ...

  7. C#获得MAC地址(网卡序列号)代码

    代码如下: //获得网卡序列号 //MAc地址 http://www.cnblogs.com/sosoft/ public string GetMoAddress() { string MoAddre ...

  8. 获取设备的mac地址可靠的方法

    参考自:http://www.open-open.com/lib/view/open1433406847322.html /** * 获取设备的mac地址 * * @param ac * @param ...

  9. java获取本机IP地址

    转载自:http://blog.csdn.net/thunder09/article/details/5360251 在网上找了几个用java获取本机IP地址的代码,发现都少都有些不完美,自己整理了一 ...

  10. PHP获取服务器的mac地址类

    PHP获取服务器的mac地址类,不是客户端的. <?php class GetMacAddr{ var $return_array = array(); // 返回带有MAC地址的字串数组 va ...

随机推荐

  1. Redis集群搭建的三种方式

    一.Redis主从 1.1 Redis主从原理 和MySQL需要主从复制的原因一样,Redis虽然读取写入的速度都特别快,但是也会产生性能瓶颈,特别是在读压力上,为了分担压力,Redis支持主从复制. ...

  2. Flutter 粘合剂CustomScrollView控件

    老孟导读:快乐的51假期结束了,切换为努力模式,今天给大家分享CustomScrollView组件,此组件在以后的项目中会经常用到,CustomScrollView就像一个粘合剂,将多个组件粘合在一起 ...

  3. 麦基数(p1045)

    描述: \(计算2^{P}−1的位数和最后500位数字(用十进制高精度数表示)\) Ⅰ.求位数 \(因为2^p最后一位必定不为0,求2^p-1的位数也就是求2^p位数\) \(2^p的位数确实很难求, ...

  4. Java 8 CompletableFuture思考

    Java 8 CompletableFuture思考 最近一直在用响应式编程写Java代码,用的框架大概上有WebFlux(Spring).R2dbc.Akka...一些响应式的框架. 全都是Java ...

  5. Unity 游戏框架搭建 2019 (四十六) 简易消息机制 & 集成到 MonoBehaviourSimplify 里

    在上一篇,我们接触了单例,使用单例解决了我们脚本之间访问的问题. 脚本之间访问其实有更好的方式. 我们先分下脚本访问脚本的几种形式. 第一种,A GameObject 是 B GameObject 的 ...

  6. 一步步打造QQ群发消息群发器

    最近为了做公众号号推广,吸粉,然后加了几百个QQ群,感觉QQ群的群发效果还是不错的,一天能捞到100个粉丝左右,好的时候也有200个,少的时候几十个,但是由于太多的群了,手工一个个点击开来群发,几百个 ...

  7. 什么情况下不能使用 Java 泛型

    1. 前言 Java 1.5 引入了泛型来保证类型安全,防止在运行时发生类型转换异常,让类型参数化,提高了代码的可读性和重用率.但是有些情况下泛型也是不允许使用的,今天就总结一下编码中不能使用泛型的一 ...

  8. 设计模式之GOF23状态模式

    状态模式state 场景:当具有许多状态并且需要频繁改变时,用这种模式 -电梯的运行:维修,正常,自动关门,自动开门,向上运行,向下运行,消防状态 -红绿灯:红灯,黄灯,绿灯 -企业或政府系统:公文的 ...

  9. spark优化总结

    1.Spark调优背景 目前Zeppelin已经上线一段时间,Spark作为底层SQL执行引擎,需要进行整体性能调优,来提高SQL查询效率.本文主要给出调优的结论,因为涉及参数很多,故没有很细粒度调优 ...

  10. AndroidStudio3.6升级后的坑-apk打包

    前段时间尝试了最新版的AndroidStudio3.6,整体来说gradle调试和自带的虚拟机相比较历史版本有了更香的体验. 刚好有个新项目,就直接使用最新版了,这次新版的升级除了保持原有的界面风格, ...