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 ...
随机推荐
- [转]使用fdisk磁盘分区和 Linux 文件系统
概述 在本文中,学习磁盘分区和 Linux 文件系统相关内容.学习: 创建分区 使用 mkfs 命令来设置 ext2.ext3.ext4.xfs.Reiser v3 和 vfat 文件系统 创建和管理 ...
- jquery serializeArray() 方法通过序列化表单值来创建对象数组(名称和值)。
serializeArray() 方法序列化表单元素(类似 .serialize() 方法),返回 JSON 数据结构数据. html代码: <form> <div><i ...
- java基础语法——方法,static关键字
一:方法: 1.什么是方法: 通俗地讲,方法就是行为.它是完成特定功能的代码块能执行一个功能.它包含于类和对象中. 2.为什么要有方法: *提高代码的复用性. *提高效率 *利于程序维护 3.命名规则 ...
- cors跨域深刻理解
1.跨域问题只出现在前端和后端不在同一个主机上.前后端在同一个主机上不会出现跨域问题. 2.浏览器的一种自我保护机制,不允许出现本地浏览器ajax异步请求访问127.0.0.1以外的系统,因为浏览器不 ...
- Android 自己定义UI文章汇总
<Android ListView分类/分组效果 - 第一种实现方式> <Android ListView分类/分组效果 - 另外一种实现方式> <Android Lis ...
- [Bash] Find Files and Folders with `find` in Bash
find is a powerful tool that can not only find files but it can run a command on each matching file ...
- cocos2dx 制作单机麻将(五)
cocos2dx 制作单机麻将(五) 麻将逻辑6 最基础的4人麻将逻辑(轮流循环出牌, 之前学的都能用上 跑起来了!!!) 最基础的麻将逻辑 依据自己须要 设置麻将人数GAME_PLAYER 基本流 ...
- [Android]自己定义带删除输入框
在项目开发中,带删除button输入框也是人们经常常使用到的,该文章便介绍一下怎样创建一个带删除输入框.当中,须要解决的问题例如以下: a)创建自己定义editText类 b)在自己定义editTex ...
- 使用python转换markdown to html
起因 有很多编辑器可以直接将markdown转换成html,为什么还要自己写呢?因为我想写完markdown之后,即可以保存在笔记软件中(比如有道),又可以放到github进行版本管理,还可以发布到博 ...
- 大数据处理之道 (htmlparser获取数据<一>)
一:简单介绍 (1)HTML Parser是一个用于解析Html的Java的库.可採用线性或嵌套两种方式.主要用于网页的转换或提取,他有一些特性:过滤器filter,遍历器visitors,通常的标签 ...