java支持跨平台获取cpuid、主板id、硬盘id、mac地址 (兼容windows、Linux)
windows:
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
java支持跨平台获取cpuid、主板id、硬盘id、mac地址 (兼容windows、Linux)的更多相关文章
- PHP中获取内网用户MAC地址(WINDOWS/linux)的实现代码
做一个内网根据MAC地址自动登录的应用,在WINDOW 2003可以正常使用,函数如下 复制代码 代码如下: function ce_getmac() { if(PHP_OS == 'WINNT' ...
- C#/VB.NET 获取电脑属性(硬盘ID、硬盘容量、Cpu序列号、MAC地址、系统类型)
在开发过程中,经常需要获取电脑的一些属性,如获取硬盘ID/CPU序列号/MAC地址作为来加密字符串. 1.硬盘 在我查看网上一些文档时,发现很多人对硬盘序列号很模糊~ 什么叫硬盘序列号?指的是作为一个 ...
- C# 获取本机CPU序列号,MAC地址,硬盘ID,本机IP地址,计算机名,物理内存,PC类型
首先引入服务 然后 调用 本文转载自http://blog.sina.com.cn/s/blog_7eeb43210101hf7f.html public class Computer { publi ...
- Java获取本机的IP与MAC地址
有些机器有许多虚拟的网卡,获取IP地址时会出现一些意外,所以需要一些验证: // 获取mac地址 public static String getMacAddress() { try { Enumer ...
- C# CPU,硬盘,mac地址灯本地信息查询
public class Computer { public static string CpuID; //1.cpu序列号 public static string MacAddress; //2. ...
- 获取客户机的ip和mac地址
只获取clientIP package com.ppms.utils; import javax.servlet.http.HttpServletRequest; /** * Created by l ...
- 使用ARP获取局域网内设备IP和MAC地址
根据Arp列表数据,查询本地设备在线状态 使用 arp -a 获得所有内网地址,首先看Mod对象 public struct MacIpPair { public string HostName; p ...
- iOS开发——获取手机当前WiFi名和MAC地址
获取手机WiFi信息. iOS9以前的方法,还是能用,警告就警告吧!iOS9以后使用的是苹果最新的API框架,NetworkExtension/NEHotspotHelper.h,这个框架,第一次开放 ...
- C#获取路由器外网IP,MAC地址
C#实现的获取路由器MAC地址,路由器外网地址.对于要获取路由器MAC地址,一定需要知道路由器web管理系统的用户名和密码.至于获取路由器的外网IP地址,可以不需要知道路由器web管理系统的用户名和密 ...
随机推荐
- 【转载】PHP性能优化干货
PHP优化对于PHP的优化主要是对php.ini中的相关主要参数进行合理调整和设置,以下我们就来看看php.ini中的一些对性能影响较大的参数应该如何设置. # vi /etc/php.ini (1) ...
- 整理几篇比较好的AndroidUI动画开发文章
第一篇,写的比较详细,新手可以看得懂: http://www.360doc.com/content/16/0128/08/30422106_531162539.shtml
- Java报表工具FineReport导出EXCEL的四种API
在实际的应用中会经常需要将数据导出成excel,导出的方式除原样导出还有分页导出.分页分sheet导出和大数据量导出.对于excel 2003版,由于限制了每个sheet的最大行数和列数,大数据量导出 ...
- 理解 OpenStack 高可用(HA)(1):OpenStack 高可用和灾备方案 [OpenStack HA and DR]
本系列会分析OpenStack 的高可用性(HA)概念和解决方案: (1)OpenStack 高可用方案概述 (2)Neutron L3 Agent HA - VRRP (虚拟路由冗余协议) (3)N ...
- 微信公共号的PHP开发(基础篇)——玩一下
最近没事儿开了个微信号,写点东西给家人啊什么的看,你们想看的话就这个嘛: 然后就意外的看到imooc上的微信公众号开发相关了.每天科研的累累的,做点这个不是很累的东西吧. 微信公共号开发 一.基础 1 ...
- Linux 之 目录和文件
1 初识 1.1 终端 打开终端: Ctrl + Alt + F1 ~ F6 图形界面: Ctrl + Alt + F7 1.2 命令 1) 一般 date; cal - calendar; ...
- AC日记——最大数 洛谷 P1198 [JSOI2008]
题目描述 现在请求你维护一个数列,要求提供以下两种操作: 1. 查询操作. 语法:Q L 功能:查询当前数列中末尾L个数中的最大的数,并输出这个数的值. 限制:L不超过当前数列的长度. 2. 插入操作 ...
- Spring知识点提炼
原文出处: 朱小厮 1. Spring框架的作用 轻量:Spring是轻量级的,基本的版本大小为2MB 控制反转:Spring通过控制反转实现了松散耦合,对象们给出它们的依赖,而不是创建或查找依赖的对 ...
- JSLint检测Javascript语法规范
前端javascript代码编写中,有一个不错的工具叫JSLint,可以检查代码规范化,压缩JS,CSS等,但是他的语法规范检查个人觉得太“苛刻”了,会提示各种各样的问题修改建议,有时候提示的信息我们 ...
- P1546 最短网络 Agri-Net
题目背景 农民约翰被选为他们镇的镇长!他其中一个竞选承诺就是在镇上建立起互联网,并连接到所有的农场.当然,他需要你的帮助. 题目描述 约翰已经给他的农场安排了一条高速的网络线路,他想把这条线路共享给其 ...