java支持跨平台获取cpuid、主板id、硬盘id、mac地址 (兼容windows、Linux)
Posted on 2014-04-01 18:38 季枫 阅读(474) 评论(0) 编辑 收藏 举报1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 | package cn.net.comsys.helper.system.info; import java.io.BufferedReader; import java.io.File; import java.io.FileWriter; import java.io.InputStreamReader; import java.io.LineNumberReader; public class HardWareUtils { /** * 获取主板序列号 * * @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 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; } public static void main(String[] args) { System.out.println( "CPU SN:" + HardWareUtils.getCPUSerial()); System.out.println( "主板 SN:" + HardWareUtils.getMotherboardSN()); System.out.println( "C盘 SN:" + HardWareUtils.getHardDiskSN( "c" )); System.out.println( "MAC SN:" + HardWareUtils.getMac()); } } |
Linux:
命令:
0、查看CPUID:dmidecode -t processor | grep 'ID'
1、查看服务器型号:dmidecode | grep 'Product Name'
2、查看主板的序列号:dmidecode |grep 'Serial Number'
3、查看系统序列号:dmidecode -s system-serial-number
4、查看内存信息:dmidecode -t memory
5、查看OEM信息:dmidecode -t 11
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 智能桌面机器人:用.NET IoT库控制舵机并多方法播放表情
· Linux glibc自带哈希表的用例及性能测试
· 深入理解 Mybatis 分库分表执行原理
· 如何打造一个高并发系统?
· .NET Core GC压缩(compact_phase)底层原理浅谈
· 手把手教你在本地部署DeepSeek R1,搭建web-ui ,建议收藏!
· 新年开篇:在本地部署DeepSeek大模型实现联网增强的AI应用
· 程序员常用高效实用工具推荐,办公效率提升利器!
· Janus Pro:DeepSeek 开源革新,多模态 AI 的未来
· 【译】WinForms:分析一下(我用 Visual Basic 写的)
2011-04-01 oracle修改数据库实例字符集
2010-04-01 linux 端口占用查看 结束占用端口的程序,windows端口占用查询