1. import java.io.File;
  2. import java.io.FileWriter;
  3. import java.io.BufferedReader;
  4. import java.io.IOException;
  5. import java.io.InputStreamReader;
  6.  
  7. import com.tjhq.hqoa.utils.Log4jUtil;
  8. import com.tjhq.hqoa.utils.StringUtil;
  9.  
  10. //取主板序列号
  11. public class MainBordUtil {
  12. /**
  13. * 获取当前操作系统名称. return 操作系统名称 例如:windows xp,linux 等.
  14. */
  15. public static String getOSName() {
  16. return System.getProperty("os.name").toLowerCase();
  17. }
  18.  
  19. public static String getMainBordId_windows() {
  20. String result = "";
  21. try {
  22. File file = File.createTempFile("realhowto", ".vbs");
  23. file.deleteOnExit();
  24. FileWriter fw = new java.io.FileWriter(file);
  25.  
  26. String vbs = "Set objWMIService = GetObject(\"winmgmts:\\\\.\\root\\cimv2\")\n"
  27. + "Set colItems = objWMIService.ExecQuery _ \n"
  28. + " (\"Select * from Win32_BaseBoard\") \n"
  29. + "For Each objItem in colItems \n"
  30. + " Wscript.Echo objItem.SerialNumber \n"
  31. + " exit for ' do the first cpu only! \n" + "Next \n";
  32.  
  33. fw.write(vbs);
  34. fw.close();
  35. Process p = Runtime.getRuntime().exec(
  36. "cscript //NoLogo " + file.getPath());
  37. BufferedReader input = new BufferedReader(new InputStreamReader(
  38. p.getInputStream()));
  39. String line;
  40. while ((line = input.readLine()) != null) {
  41. result += line;
  42. }
  43. input.close();
  44. } catch (Exception e) {
  45. Log4jUtil.error("获取主板信息错误",e);
  46. }
  47. return result.trim();
  48. }
  49.  
  50. public static String getMainBordId_linux() {
  51.  
  52. String result = "";
  53. String maniBord_cmd = "dmidecode | grep 'Serial Number' | awk '{print $3}' | tail -1";
  54. Process p;
  55. try {
  56. p = Runtime.getRuntime().exec(
  57. new String[] { "sh", "-c", maniBord_cmd });// 管道
  58. BufferedReader br = new BufferedReader(new InputStreamReader(
  59. p.getInputStream()));
  60. String line;
  61. while ((line = br.readLine()) != null) {
  62. result += line;
  63. break;
  64. }
  65. br.close();
  66. } catch (IOException e) {
  67. Log4jUtil.error("获取主板信息错误",e);
  68. }
  69. return result;
  70. }
  71.  
  72. public static String getMainBordId() throws Exception {
  73. String os = getOSName();
  74. String mainBordId = "";
  75. if (os.startsWith("windows")) {
  76. mainBordId = getMainBordId_windows();
  77. } else if (os.startsWith("linux")) {
  78. mainBordId = getMainBordId_linux();
  79. }
  80. if(!StringUtil.isNotNullOrBlank(mainBordId)){
  81. mainBordId="null";
  82. }
  83. return mainBordId;
  84. }
  85.  
  86. public static void main(String[] args) throws Exception {
  87. String mainBord = getMainBordId();
  88. System.out.println(mainBord);
  89. }
  90. }
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4. import java.net.InetAddress;
  5. import java.net.NetworkInterface;
  6. import java.util.ArrayList;
  7. import java.util.Enumeration;
  8. import java.util.List;
  9.  
  10. import com.tjhq.hqoa.utils.Log4jUtil;
  11. import com.tjhq.hqoa.utils.StringUtil;
  12.  
  13. /**
  14. * 与系统相关的一些常用工具方法.
  15. *
  16. * @version 1.0.0
  17. */
  18. public class MACUtil {
  19.  
  20. /**
  21. * 获取当前操作系统名称. return 操作系统名称 例如:windows xp,linux 等.
  22. */
  23. public static String getOSName() {
  24. return System.getProperty("os.name").toLowerCase();
  25. }
  26.  
  27. /**
  28. * 获取unix网卡的mac地址. 非windows的系统默认调用本方法获取. 如果有特殊系统请继续扩充新的取mac地址方法.
  29. *
  30. * @return mac地址
  31. */
  32. public static String getMAC_linux() {
  33. String mac = null;
  34. BufferedReader bufferedReader = null;
  35. Process process = null;
  36. try {
  37. // linux下的命令,一般取eth0作为本地主网卡
  38. process = Runtime.getRuntime().exec("ifconfig eth0");
  39. // 显示信息中包含有mac地址信息
  40. bufferedReader = new BufferedReader(new InputStreamReader(
  41. process.getInputStream()));
  42. String line = null;
  43. int index = -1;
  44. while ((line = bufferedReader.readLine()) != null) {
  45. // 寻找标示字符串[hwaddr]
  46. index = line.toLowerCase().indexOf("hwaddr");
  47. if (index >= 0) {// 找到了
  48. // 取出mac地址并去除2边空格
  49. mac = line.substring(index + "hwaddr".length() + 1).trim();
  50. break;
  51. }
  52. }
  53. } catch (IOException e) {
  54. Log4jUtil.error("获取mac信息错误",e);
  55. } finally {
  56. try {
  57. if (bufferedReader != null) {
  58. bufferedReader.close();
  59. }
  60. } catch (IOException e1) {
  61. Log4jUtil.error("获取mac信息错误",e1);
  62. }
  63. bufferedReader = null;
  64. process = null;
  65. }
  66. return mac;
  67. }
  68.  
  69. /**
  70. * 获取widnows网卡的mac地址.
  71. *
  72. * @return mac地址
  73. */
  74. public static String getMAC_windows() {
  75. InetAddress ip = null;
  76. NetworkInterface ni = null;
  77. List<String> macList = new ArrayList<String>();
  78. try {
  79. Enumeration<NetworkInterface> netInterfaces = (Enumeration<NetworkInterface>) NetworkInterface
  80. .getNetworkInterfaces();
  81. while (netInterfaces.hasMoreElements()) {
  82. ni = (NetworkInterface) netInterfaces.nextElement();
  83. // ----------特定情况,可以考虑用ni.getName判断
  84. // 遍历所有ip
  85. Enumeration<InetAddress> ips = ni.getInetAddresses();
  86. while (ips.hasMoreElements()) {
  87. ip = (InetAddress) ips.nextElement();
  88. if (!ip.isLoopbackAddress() // 非127.0.0.1
  89. && ip.getHostAddress().matches(
  90. "(\\d{1,3}\\.){3}\\d{1,3}")) {
  91. macList.add(getMacFromBytes(ni.getHardwareAddress()));
  92. }
  93. }
  94. }
  95. } catch (Exception e) {
  96. Log4jUtil.error("获取mac错误", e);
  97. }
  98. if (macList.size() > 0) {
  99. return macList.get(0);
  100. } else {
  101. return "";
  102. }
  103.  
  104. }
  105.  
  106. private static String getMacFromBytes(byte[] bytes) {
  107. StringBuffer mac = new StringBuffer();
  108. byte currentByte;
  109. boolean first = false;
  110. for (byte b : bytes) {
  111. if (first) {
  112. mac.append("-");
  113. }
  114. currentByte = (byte) ((b & 240) >> 4);
  115. mac.append(Integer.toHexString(currentByte));
  116. currentByte = (byte) (b & 15);
  117. mac.append(Integer.toHexString(currentByte));
  118. first = true;
  119. }
  120. return mac.toString().toUpperCase();
  121. }
  122.  
  123. public static String getMAC() throws Exception {
  124. String os = getOSName();
  125. String mac = "";
  126. if (os.startsWith("windows")) {
  127. mac = getMAC_windows();
  128. } else if (os.startsWith("linux")) {
  129. mac = getMAC_linux();
  130. }
  131. if(!StringUtil.isNotNullOrBlank(mac)){
  132. mac="null";
  133. }
  134. return mac;
  135. }
  136.  
  137. /**
  138. * 测试用的main方法.
  139. *
  140. * @param argc
  141. * 运行参数.
  142. * @throws Exception
  143. */
  144. public static void main(String[] argc) throws Exception {
  145. String mac = getMAC();
  146. System.out.println(mac);
  147. }
  148. }
  1. import java.io.BufferedReader;
  2. import java.io.File;
  3. import java.io.FileWriter;
  4. import java.io.IOException;
  5. import java.io.InputStreamReader;
  6.  
  7. import com.tjhq.hqoa.utils.Log4jUtil;
  8. import com.tjhq.hqoa.utils.StringUtil;
  9.  
  10. public class CPUUtil {
  11. /**
  12. * 获取当前操作系统名称. return 操作系统名称 例如:windows xp,linux 等.
  13. */
  14. public static String getOSName() {
  15. return System.getProperty("os.name").toLowerCase();
  16. }
  17.  
  18. /**
  19. * 获取CPU序列号
  20. *
  21. * @return
  22. */
  23. public static String getCPUID_Windows() {
  24. String result = "";
  25. try {
  26. File file = File.createTempFile("tmp", ".vbs");
  27. file.deleteOnExit();
  28. FileWriter fw = new java.io.FileWriter(file);
  29. String vbs = "Set objWMIService = GetObject(\"winmgmts:\\\\.\\root\\cimv2\")\n"
  30. + "Set colItems = objWMIService.ExecQuery _ \n"
  31. + " (\"Select * from Win32_Processor\") \n"
  32. + "For Each objItem in colItems \n"
  33. + " Wscript.Echo objItem.ProcessorId \n"
  34. + " exit for ' do the first cpu only! \n" + "Next \n";
  35.  
  36. // + " exit for \r\n" + "Next";
  37. fw.write(vbs);
  38. fw.close();
  39. Process p = Runtime.getRuntime().exec(
  40. "cscript //NoLogo " + file.getPath());
  41. BufferedReader input = new BufferedReader(new InputStreamReader(
  42. p.getInputStream()));
  43. String line;
  44. while ((line = input.readLine()) != null) {
  45. result += line;
  46. }
  47. input.close();
  48. file.delete();
  49. } catch (Exception e) {
  50. Log4jUtil.error("获取cpu信息错误",e);
  51. }
  52. return result.trim();
  53. }
  54.  
  55. public static String getCPUID_linux() throws InterruptedException {
  56. String result = "";
  57. String CPU_ID_CMD = "dmidecode";
  58. BufferedReader bufferedReader = null;
  59. Process p = null;
  60. try {
  61. p = Runtime.getRuntime().exec(new String[]{ "sh", "-c", CPU_ID_CMD });// 管道
  62. bufferedReader = new BufferedReader(new InputStreamReader(
  63. p.getInputStream()));
  64. String line = null;
  65. int index = -1;
  66. while ((line = bufferedReader.readLine()) != null) {
  67. // 寻找标示字符串[hwaddr]
  68. index = line.toLowerCase().indexOf("uuid");
  69. if (index >= 0) {// 找到了
  70. // 取出mac地址并去除2边空格
  71. result = line.substring(index + "uuid".length() + 1).trim();
  72. break;
  73. }
  74. }
  75.  
  76. } catch (IOException e) {
  77. Log4jUtil.error("获取cpu信息错误",e);
  78. }
  79. return result.trim();
  80. }
  81.  
  82. public static String getCPUId() throws InterruptedException {
  83. String os = getOSName();
  84. String cpuId = "";
  85. if (os.startsWith("windows")) {
  86. cpuId = CPUUtil.getCPUID_Windows();
  87. } else if (os.startsWith("linux")) {
  88. cpuId = CPUUtil.getCPUID_linux();
  89. }
  90. if(!StringUtil.isNotNullOrBlank(cpuId)){
  91. cpuId="null";
  92. }
  93. return cpuId;
  94. }
  95.  
  96. public static void main(String[] args) throws Exception {
  97. String os = getOSName();
  98. System.out.println(os);
  99. String cpuid = getCPUId();
  100. System.out.println(cpuid);
  101. }
  102.  
  103. }

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. CodeForces - 1245 B - Restricted RPS(贪心)

    Codeforces Round #597 (Div. 2) Let nn be a positive integer. Let a,b,ca,b,c be nonnegative integers ...

  2. RF(数据库测试)

    1.下载 DatabaseLibrary 库 pip install robotframework-databaselibrary 2.下载 pymysql 库(作为中间件) pip install ...

  3. Docker docker-compose 配置lnmp开发环境

    1.安装docker yum -y install dockersystemctl start dockersystemctl enable docker 安装docker-compose https ...

  4. Fibonacci相关问题

    公式如下: 递归的解法我就不写了,贴一个递推的. long long Fibonacci(unsigned int n) { ) ; ) ; ; ; long long res; ; i <= ...

  5. Flutter 粘合剂CustomScrollView控件

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

  6. 完全背包和多重背包的混合 F - The Fewest Coins

    http://poj.org/problem?id=3260 这个题目有点小难,我开始没什么头绪,感觉很乱. 后来看了题解,感觉豁然开朗. 题目大意:就是这个人去买东西,东西的价格是T,这个人拥有的纸 ...

  7. vue项目兼容ie

    一.兼容ES6 Vue 的核心框架 vuejs 本身,以及官方核心插件(VueRouter.Vuex等)均可以在 ie9 上正常使用.但ie不兼容es6,所以需要安装插件将“Promise”等高级语法 ...

  8. restful 架构风格的curd(增删改查)

    restful架构 概念:REST指的是一组架构约束条件和原则,如果一个架构符合REST的约束条件和原则,就称之为RESTful架构. restful不是一个专门的技术,他是一个规范.规范就是写写代码 ...

  9. 关于SpringBoot的外部化配置使用记录

    关于SpringBoot的外部化配置使用记录 声明: 若有任何纰漏.错误请不吝指出! 记录下使用SpringBoot配置时遇到的一些麻烦,虽然这种麻烦是因为知识匮乏导致的. 记录下避免一段时间后自己又 ...

  10. java ->IO流_字符流

    字符流 经过前面的学习,我们基本掌握的文件的读写操作,在操作过程中字节流可以操作所有数据,可是当我们操作的文件中有中文字符,并且需要对中文字符做出处理时怎么办呢? 字节流读取字符的问题 通过以下程序读 ...