获取cpu序列号

/*

* 文 件 名: ExcCommand.java 版 权: Unis Cloud Information Technology Co., Ltd.

* Copyright 2017, All rights reserved 描 述: 修 改 人: Administrator 修改时间:

* 2017年11月14日 跟踪单号: 修改单号: 修改内容:

*/

package com.hubin.cpunum;

import java.io.BufferedReader;

import java.io.File;

import java.io.FileWriter;

import java.io.IOException;

import java.io.InputStream;

import java.io.InputStreamReader;

import java.net.InetAddress;

import java.net.NetworkInterface;

import java.util.ArrayList;

import java.util.Enumeration;

import java.util.Scanner;

public class ExcCommand {

public static boolean isRegister = false;

// 机器序列号

private static String machineCode;

/**

* @return 返回 machineCode

*/

public static String getMachineCode() {

if (machineCode == null) {

machineCode = getSn();

}

return machineCode;

}

/**

* @param 对machineCode进行赋值

*/

public static void setMachineCode(String machineCode) {

ExcCommand.machineCode = machineCode;

}

public static String getSn() {

String cid = "EEEE";

String hid = "EEEE";

String osName = System.getProperty("os.name").toLowerCase();

if (osName.indexOf("win") > -1) {// windows系统

cid = getCPUSerialInWindows();

hid = getHardDiskSN("c");

}

else {// Linux系统

cid = getCPUSerial();

hid = getMBId();

}

if (hid.length() < 4) {

hid = "EEEE";

}

/*

* String mac = "EEEE"; try { String allMac = getMACAddress();

* System.out.println("mac:"+allMac); mac = allMac.substring(8); } catch

* (Exception e) { e.printStackTrace(); } //

*/

String sn = cid + hid.substring(hid.length() - 4);

sn = sn.substring(0, 4) + "-" + sn.substring(4, 8) + "-" + sn.substring(8, 12) + "-" + sn.substring(12);

return sn;

}

/**

* 获取CPU序列号,windows系统

*

* @return

*/

public static String getCPUSerialInWindows() {

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被读取";

}

else {

result = result.trim();

}

return result;

}

/**

* 获取CUP序列号 linux系统

*

* @return [参数说明]

* @return String [返回类型说明]

* @exception throws [违例类型] [违例说明]

* @see [类、类#方法、类#成员]

*/

public static String getCPUSerial() {

String osName = System.getProperty("os.name").toLowerCase();

String result[] = new String[2];

String para[] = new String[3];

ArrayList param = new ArrayList();

String cpuid = null;

System.out.println("osName:" + osName);

try {

if (osName.indexOf("windows 9") > -1) {

param.add("command.com");

// ignore win9

/*

* param.add("/c"); param.add("wmic cpu get ProcessorId");

* result = ExcCommand.exec((String[])param.toArray()); //

*/

}

else if (osName.indexOf("win") > -1) {

param.add("cmd.exe");

param.add("/c");

param.add("wmic cpu get ProcessorId");

result = exec(param.toArray(para));

if (0 < result[1].length()) {

System.out.println("error info:---------------\r\n" + result[1]);

}

result[0] = result[0].trim();

cpuid = result[0].substring(result[0].lastIndexOf('\n') + 1);

}

else {

// Linux,Unix

param.add("/bin/sh");

param.add("-c");

param.add("dmidecode -t processor |grep 'ID'");

result = exec(param.toArray(para));

if (0 < result[1].length()) {

System.out.println("error info:---------------\r\n" + result[1]);

}

cpuid = result[0].substring(result[0].indexOf(' ') + 1, result[0].indexOf('\n'));

cpuid = cpuid.replace(" ", "");

}

}

catch (Throwable e) {

e.printStackTrace();

return "";

}

// if (16 == cpuid.length()) {

// cpuid = cpuid.substring(0, 4) + "-" + cpuid.substring(4, 8) + "-" +

// cpuid.substring(8, 12) + "-"

// + cpuid.substring(12);

// }

return cpuid;

}

/**

* 获取硬盘序列号,windows系统

*

* @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();

file.delete();

}

catch (Exception e) {

e.printStackTrace();

}

return result.trim();

}

// 获取linux系统的主板id

public static String getMBId() {

String result[] = new String[2];

String para[] = new String[3];

ArrayList param = new ArrayList();

String mbid = "EEEE";

param.add("/bin/sh");

param.add("-c");

param.add("dmidecode |grep 'Serial Number'");

try {

result = exec(param.toArray(para));

if (0 < result[1].length()) {

System.out.println("error info:---------------\r\n" + result[1]);

}

String[] sn = result[0].split("Serial Number: ");

String ms;

for (int i = 0; i < sn.length; i++) {

ms = sn[i].trim();

if ("Not Specified".equalsIgnoreCase(ms)) {

continue;

}

if (10 == ms.length()) {

mbid = ms;

break;

}

}

}

catch (Throwable e) {

e.printStackTrace();

}

return mbid;

}

/**

* 获取CUP序列号

*

* @return [参数说明]

* @return String [返回类型说明]

* @exception throws [违例类型] [违例说明]

* @see [类、类#方法、类#成员]

*/

public static String getCPU() {

Process process;

String serial = null;

try {

process = Runtime.getRuntime().exec(new String[] { "wmic", "cpu", "get", "ProcessorId" });

process.getOutputStream().close();

Scanner sc = new Scanner(process.getInputStream());

String property = sc.next();

serial = sc.next();

}

catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

return serial;

}

public static String getLocalMACAddress() throws Exception {

InetAddress ia = InetAddress.getLocalHost();// 获取本地IP对象

// 获得网络接口对象(即网卡),并得到mac地址,mac地址存在于一个byte数组中。

byte[] mac = NetworkInterface.getByInetAddress(ia).getHardwareAddress();

// 下面代码是把mac地址拼装成String

StringBuffer sb = new StringBuffer();

for (int i = 0; i < mac.length; i++) {

// if (i != 0) {

// sb.append("-");

// }

// mac[i] & 0xFF 是为了把byte转化为正整数

String s = Integer.toHexString(mac[i] & 0xFF);

sb.append(s.length() == 1 ? 0 + s : s);

}

// 把字符串所有小写字母改为大写成为正规的mac地址并返回

return sb.toString().toUpperCase();

}

// 获取MAC地址的方法

public static String getMACAddress() throws Exception {

InetAddress ia = InetAddress.getLocalHost();// 获取本地IP对象

// 获得网络接口对象(即网卡),并得到mac地址,mac地址存在于一个byte数组中。

Enumeration nis = NetworkInterface.getNetworkInterfaces();

String macRes = "EEEEEEEEEEEE";

// 下面代码是把mac地址拼装成String

StringBuffer sb;

while (nis.hasMoreElements()) {

byte[] mac = nis.nextElement().getHardwareAddress();// NetworkInterface.getByInetAddress(ia)

if (null != mac && 0 < mac.length) {

sb = new StringBuffer();

for (int i = 0; i < mac.length; i++) {

/*

* if (i != 0) { sb.append("-"); } //

*/

// mac[i] & 0xFF 是为了把byte转化为正整数

String s = Integer.toHexString(mac[i] & 0xFF);

sb.append(s.length() == 1 ? 0 + s : s);

}

macRes = sb.toString().toUpperCase();

// System.out.println("get MAC:" + macRes);

// return sb.toString().toUpperCase();

}

}

return macRes;

}

/**

* 返回命令执行结果信息串

*

* @param command 要执行的命令

* @return 第一个为标准信息,第二个为错误信息

* @throws Throwable String[]

*/

public static String[] exec(String[] command) throws Throwable {

Process process = null;

Runtime runtime = Runtime.getRuntime();

// Linux,Unix

process = runtime.exec(command);

// 存储返回结果,第一个为标准信息,第二个为错误信息

String result[] = new String[2];

ReadThread inputReadThread = new ReadThread(process.getInputStream());

ReadThread errReadThread = new ReadThread(process.getErrorStream());

inputReadThread.start();

errReadThread.start();

// 确保标准与错误流都读完时才向外界返回执行结果

while (true) {

if (inputReadThread.flag && errReadThread.flag) {

break;

}

else {

Thread.sleep(200);

}

}

result[0] = inputReadThread.getResult();

result[1] = errReadThread.getResult();

return result;

}

public static void main(String args[]) throws Throwable {

System.out.println(getSn());

}

/*

* 标准流与错误流读取线程

*/

private static class ReadThread extends Thread {

private InputStream is;

private ArrayList result = new ArrayList();

public boolean flag;// 流是否读取完毕

public ReadThread(InputStream is) {

this.is = is;

}

// 获取命令执行后输出信息,如果没有则返回空""字符串

protected String getResult() {

byte[] byteArr = new byte[result.size()];

for (int i = 0; i < result.size(); i++) {

byteArr[i] = ((Byte) result.get(i)).byteValue();

}

return new String(byteArr);

}

public void run() {

try {

int readInt = is.read();

while (readInt != -1) {

result.add(Byte.valueOf(String.valueOf((byte) readInt)));

readInt = is.read();

}

flag = true;// 流已读完

}

catch (Exception e) {

e.printStackTrace();

}

}

}

}

matlab cpu序列号,获取cpu序列号 - huhaoren的个人空间 - OSCHINA - 中文开源技术交流社区...相关推荐

  1. 微信步数C语言程序,获取微信步数 - osc_1v2pb1nt的个人空间 - OSCHINA - 中文开源技术交流社区...

    const util = require('../../utils/util.js') Page({ /** * 页面的初始数据 */ data: { runData:[], }, /** * 生命周 ...

  2. TF-IDF的Matlab程序,TF-IDF算法简析 - osc_sjg81se7的个人空间 - OSCHINA - 中文开源技术交流社区...

    TF-IDF算法可用来提取文档的关键词,关键词在文本聚类.文本分类.文献检索.自动文摘等方面有着重要应用. 算法原理 TF:Term Frequency,词频 IDF:Inverse Document ...

  3. matlab求轮廓系数,K-mean算法分析 - osc_b2jf5efr的个人空间 - OSCHINA - 中文开源技术交流社区...

    from sklearn.datasets import load_sample_image from sklearn.cluster import KMeans import matplotlib. ...

  4. 精英主义 遗传算法 matlab,遗传算法优化 - osc_lfs4vsih的个人空间 - OSCHINA - 中文开源技术交流社区...

    1.遗传算法简介 遗传算法是一种基于自然选择和群体遗传机理的搜索算法,它模拟了自然选择和自然遗传过程中的繁殖.杂交和突变现象.再利用遗传算法求解问题时,问题的每一个可能解都被编码成一个"染色 ...

  5. matlab一计算自动四舍五入6,matlab总结 - osc_msmij2gf的个人空间 - OSCHINA - 中文开源技术交流社区...

    MATLAB 总结 一 基础部分 1.save('x','y','z') 保存三个变量 2.load('var') 加载变量 3.help size who dir cd delete clear 4 ...

  6. 频率域滤波matlab函数,频率域滤波(2) - osc_t0xmr011的个人空间 - OSCHINA - 中文开源技术交流社区...

    一.频率域滤波的基本步骤: 1)使用函数tofloat把输入图像转换为浮点图像(im2double函数也可以) [f,revertclass] = tofloat(f) 2)使用函数paddedsiz ...

  7. matlab提示音,matlab发出声音 - osc_45mm6g1p的个人空间 - OSCHINA - 中文开源技术交流社区...

    我有时候运行一段很长的代码,在等待的时候去做别的事,希望程序运行完可以有一个提示音. 这可以用matlab的一个函数sound实现,该函数的输入参量是音频数据向量.采样频率和转换位数. % 响一声 s ...

  8. java获取一个目录下所有字谜_Java 猜字谜游戏 - osc_4jkldo6l的个人空间 - OSCHINA - 中文开源技术交流社区...

    package fundmental_excise6; import java.util.Arrays; import java.util.Scanner; /** * @author : jeasi ...

  9. 获取腾讯视频真实地址php源码,解析腾讯视频真实地址 - osc_hajrc28s的个人空间 - OSCHINA - 中文开源技术交流社区...

    解析腾讯视频真实地址 标签(空格分隔): php 分析 1 我们从腾讯视频上找到的网页链接格式是这样的 https://v.qq.com/x/page/b0136et5ztz.html vid = b ...

  10. php获取汉字音调,PHP汉字转拼音 - xiangqian1的个人空间 - OSCHINA - 中文开源技术交流社区...

    基于 CC-CEDICT 词典的中文转拼音工具,更准确的支持多音字的汉字转拼音解决方案.安装使用Composer安装:composer require "overtrue/pinyin:~3 ...

最新文章

  1. http请求过程简要
  2. angular 前端路由不生效解决方案
  3. 自定义取值距离的javascript random()函数
  4. .net事务的困扰,请大虾指点。
  5. 【NLP】在机器学习中开发情感分析器的5种方法
  6. 关于Apache虚拟主机的设置
  7. Boost:bind绑定__cdecl(成员函数)测试程序
  8. 付费?是不可能的!20行Python代码实现一款永久免费PDF编辑工具
  9. mac安装ipython_Mac下安装ipython与jupyter
  10. 作者:李俊清,山东农业大学副教授。
  11. spring事务再研究
  12. Windows下安装MongoDB
  13. python随机生成两个一维数组_如何用python随机产生一个一维数组
  14. 博客园在我的博客添加点击小心心特效
  15. kali 安装vmware 14 for linux 出现问题
  16. rpcbind 服务启动失败
  17. 【使用Idea打包war包】
  18. BZOJ 1984: 月下“毛景树” [树链剖分 边权]
  19. 笔记本电脑麦克风没有声音的解决方法
  20. Error:间接寻址级别不同——C++真的魔鬼

热门文章

  1. csdn 积分/c币获取方法
  2. 牛腩新闻发布系统—错误总结
  3. 远程桌面命令是什么 如何使用命令连接远程桌面
  4. Sort代码详解学习
  5. Keil(MDK) 5 软件安装教程
  6. 基于C语言的图形化编程软件,图形化编程工具
  7. BPA仿真软件需要购买吗,BPA电力仿真软件教程
  8. 汇川PLC编程软件AutoShop的应用
  9. java 文件的删除一行_如何从java中的文件中删除一行文本?
  10. 三、北京54坐标系与西安80坐标系