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 ...
随机推荐
- SQLSERVER金额转换成英文大写的函数
CREATE FUNCTION [dbo].[f_num_eng] (@num numeric(15,2)) RETURNS varchar(400) WITH ENCRYPTION AS BEGIN ...
- 【POJ3294】Life Forms(后缀数组,二分)
题意: n<=100 len[i]<=1000 思路:这是一道论文题 ..]of longint; ch:..]of ansistring; n,n1,l,r,mid,last,i,j,m ...
- Event Logging 技术简介
https://blog.csdn.net/xiliang_pan/article/details/41805023
- POJ 1753 Flip Game【枚举】
题目链接: http://poj.org/problem?id=1753 题意: 由白块黑块组成的4*4方格,每次换一个块的颜色,其上下左右的块也会被换成相反的颜色.问最少换多少块,使得最终方格变为全 ...
- [Bzoj4260]Codechef REBXOR(trie树)
4260: Codechef REBXOR Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 1534 Solved: 669[Submit][Stat ...
- [洛谷U22157]刷水题(数位dp)(hash)
题目背景 做正经题是不可能做正经题的,这辈子都不可能做正经题的,毒瘤题又不会做毒瘤题,就是水题这种东西,才维持了蒟蒻的信心: 题目描述 这里有N+1 道水题,编号分别为0 ~N+1 ,每道水题都有它自 ...
- CodeForces 570D 【dfs序】
题意: 给一颗树,根节点深度为1,每一个节点都代表一个子母. 数据输入: 节点数 询问数 从编号为2的节点开始依次输入其父节点的编号(共有节点数减1个数字输入) 字符串有节点数个小写字母 接下来询问 ...
- Spring事务管理概述
以下内容引用自http://wiki.jikexueyuan.com/project/spring/transaction-management.html: 数据库事务是一个被视为单一的工作单元的操作 ...
- openwrt下载编译
1. 安装依赖包: yum install autoconf binutils bison bzip2 flex gawk gcc gcc-c++ gettext make ncurses-devel ...
- SpringBoot初始教程之日志处理(二)
SpringBoot初始教程之日志处理(二) 1.介绍 SpringBoot默认是采用logback进行日志处理.Logback是由log4j创始人设计的又一个开源日志组件.Logback是由log4 ...