java获取操作系统的MAC地址和硬盘序列号
1.判断操作系统是Windows还是Linux
private static Boolean isLinux() {
String os = System.getProperty("os.name");
log.info("os.name: {}", os);
return !os.toLowerCase().startsWith("win");
}
2. Linux:
获取MAC地址:
private static String getMACAddressByLinux() throws Exception {
String[] cmd = {"ifconfig"};
Process process = Runtime.getRuntime().exec(cmd);
process.waitFor();
BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream()));
StringBuffer sb = new StringBuffer();
String line;
while ((line = br.readLine()) != null) {
sb.append(line);
}
String str1 = sb.toString();
String str2 = str1.split("ether")[].trim();
String result = str2.split("txqueuelen")[].trim();
log.info("Linux MacAddress is: {}", result);
br.close();
return result;
}
获取硬盘序列号:
private static String getIdentifierByLinux() throws Exception {
String[] cmd = {"fdisk", "-l"};
Process process = Runtime.getRuntime().exec(cmd);
process.waitFor();
BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream()));
StringBuffer sb = new StringBuffer();
String line;
while ((line = br.readLine()) != null) {
sb.append(line);
}
String str1 = sb.toString();
String str2 = str1.split("identifier:")[].trim();
String result = str2.split("Device Boot")[].trim();
log.info("Linux Identifier is: {}", result);
br.close();
return result;
}
3. Windows:
获取MAC地址: (默认获取第一张网卡)
private static String getMACAddressByWindows() throws Exception {
String result = "";
Process process = Runtime.getRuntime().exec("ipconfig /all");
BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream(), "GBK"));
String line;
int index = -;
while ((line = br.readLine()) != null) {
index = line.toLowerCase().indexOf("物理地址");
if (index >= ) {// 找到了
index = line.indexOf(":");
if (index >= ) {
result = line.substring(index + ).trim();
}
break;
}
}
log.info("Windows MACAddress is: {}", result);
br.close();
return result;
}
获取硬盘序列号: (默认获取C盘)
private static String getIdentifierByWindows() throws Exception {
String result = "";
Process process = Runtime.getRuntime().exec("cmd /c dir C:");
BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream(), "GBK"));
String line;
while ((line = br.readLine()) != null) {
if (line.indexOf("卷的序列号是 ") != -) {
result = line.substring(line.indexOf("卷的序列号是 ") + "卷的序列号是 ".length(), line.length());
break;
}
}
log.info("Windows Identifier is: {}", result);
br.close();
return result;
}
4. 测试:
public static void main(String[] a) throws Exception {
// 判断是Linux还是Windows
if (isLinux()) {
// Linux操作系统
String macAddress = getMACAddressByLinux();
System.out.println("Linux macAddress: " + macAddress);
String Identifier = getIdentifierByLinux();
System.out.println("Linux Identifier: " + Identifier);
} else {
// Windows操作系统
String macAddress = getMACAddressByWindows();
System.out.println("Windows macAddress: " + macAddress);
String Identifier = getIdentifierByWindows();
System.out.println("Windows Identifier: " + Identifier);
}
}
注意事项:
在Windows环境使用javac Test.java 命令编译该java文件时, 需指定编码, 应使用以下命令:
javac -encoding UTF- Test.java
java获取操作系统的MAC地址和硬盘序列号的更多相关文章
- 转: 通过WMI获取网卡MAC地址、硬盘序列号、主板序列号、CPU ID、BIOS序列号
最近由于项目的需要,需要在程序中获取机器的硬盘序列号和MAC地址等信息,在C#下,可以很容易的获得这些信息,但是在C++程序中感觉比较麻烦.经过百度,发现很多大虾都是通过WMI来获取这些硬件信息的,网 ...
- windows平台下获取网卡MAC地址、硬盘序列号、主板序列号、CPU ID、BIOS序列号
转自http://blog.csdn.net/jhqin/article/details/5548656,如有侵权,请联系本人删除,谢谢!! 头文件:WMI_DeviceQuery.h /* ---- ...
- (转)通过WMI获取网卡MAC地址、硬盘序列号、主板序列号、CPU ID、BIOS序列号
最近由于项目的需要,需要在程序中获取机器的硬盘序列号和MAC地址等信息,在C#下,可以很容易的获得这些信息,但是在C++程序中感觉比较麻烦.经过百度,发现很多大虾都是通过WMI来获取这些硬件信息的,网 ...
- 通过WMI获取网卡MAC地址、硬盘序列号、主板序列号、CPU ID、BIOS序列号
转载:https://www.cnblogs.com/tlduck/p/5132738.html #define _WIN32_DCOM #include<iostream> #inclu ...
- 获取CPU序列号、网卡MAC地址、硬盘序列号
<pre name="code" class="csharp"> using System; using System.Collections; u ...
- C# 获取CPU序列号、网卡MAC地址、硬盘序列号封装类,用于软件绑定电脑
using System.Management; namespace GLaLa { /// <summary> /// hardware_mac 的摘要说明. /// </summ ...
- java 获取本地 mac 地址
主要参考:Java获取本机MAC地址/IP地址/主机名 做的更改: 1.我的windows是中文版,程序中获取mac时是按照physical address 获取的,添加上"物理地址&quo ...
- JAVA获取客户端IP地址和MAC地址
1.获取客户端IP地址 public String getIp(HttpServletRequest request) throws Exception { String ip = request.g ...
- java 通过ip获取客户端mac地址
java 通过ip获取客户端mac地址 package com.asppro.util; import java.io.BufferedReader; import java.io.IOExcepti ...
随机推荐
- [NOIP1998] 提高组 洛谷P1011 车站
题目描述 火车从始发站(称为第1站)开出,在始发站上车的人数为a,然后到达第2站,在第2站有人上.下车,但上.下车的人数相同,因此在第2站开出时(即在到达第3站之前)车上的人数保持为a人.从第3站起( ...
- msp430入门编程41
msp430中C语言的软件工程--状态机建模
- AS3实现ToolTip效果
AS3核心类中没有ToolTip类,Flex中的ToolTip类没法用在AS3工程中,Aswing的JToolTip不错,不过如果仅仅为了使用这一个类而导入Aswing就不太明智了.由于最近的项目需要 ...
- UVA 861 组合数学 递推
题目链接 https://vjudge.net/problem/UVA-861 题意: 一个国际象棋棋盘,‘象’会攻击自己所在位置对角线上的棋子.问n*n的棋盘 摆放k个互相不攻击的 '象' 有多少种 ...
- React学习及实例开发(一)——开始
本文基于React v16.4.1 初学react,有理解不对的地方,欢迎批评指正^_^ 一.构建一个新项目 1.命令行运行如下命令,构建一个新的react项目 npm install -g crea ...
- springboot 第一个程序
idea --> new project --> 选择Spirng Initializr --> next 傻瓜式操作 --> 添加web依赖 项目基本结构: 创建contr ...
- 【纯净版windows系统】U盘启动制作图文教程
无废话,按照步骤来就可以. 1.一个大于4G的U盘(格式化)准备好U盘,请注意制作过程中对U盘有格式化操作,有用的东西请先备份 2.UltraISO(软碟通软件)下载安装百度“软碟通”,或者访问 ht ...
- 附加数据库时,提示“Microsoft SQL Server,错误: 5120”, 解决方案
错误的提示内容为:
- Meteor Blaze
Blaze是Meteor 软件包用于构建现场反应模板. Render方法 这种方法被用于绘制模板到DOM.首先,我们将创建 myNewTemplate 之后渲染. 我们增加 myContainer 这 ...
- 使用mongostat监视mongodb
1, 监视一个mongod mongostat 10.80.1.1:27018 1,监视replica set mongostat --host rs0/10.80.1.1:27018,10.80.1 ...