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地址和硬盘序列号的更多相关文章

  1. 转: 通过WMI获取网卡MAC地址、硬盘序列号、主板序列号、CPU ID、BIOS序列号

    最近由于项目的需要,需要在程序中获取机器的硬盘序列号和MAC地址等信息,在C#下,可以很容易的获得这些信息,但是在C++程序中感觉比较麻烦.经过百度,发现很多大虾都是通过WMI来获取这些硬件信息的,网 ...

  2. windows平台下获取网卡MAC地址、硬盘序列号、主板序列号、CPU ID、BIOS序列号

    转自http://blog.csdn.net/jhqin/article/details/5548656,如有侵权,请联系本人删除,谢谢!! 头文件:WMI_DeviceQuery.h /* ---- ...

  3. (转)通过WMI获取网卡MAC地址、硬盘序列号、主板序列号、CPU ID、BIOS序列号

    最近由于项目的需要,需要在程序中获取机器的硬盘序列号和MAC地址等信息,在C#下,可以很容易的获得这些信息,但是在C++程序中感觉比较麻烦.经过百度,发现很多大虾都是通过WMI来获取这些硬件信息的,网 ...

  4. 通过WMI获取网卡MAC地址、硬盘序列号、主板序列号、CPU ID、BIOS序列号

    转载:https://www.cnblogs.com/tlduck/p/5132738.html #define _WIN32_DCOM #include<iostream> #inclu ...

  5. 获取CPU序列号、网卡MAC地址、硬盘序列号

    <pre name="code" class="csharp"> using System; using System.Collections; u ...

  6. C# 获取CPU序列号、网卡MAC地址、硬盘序列号封装类,用于软件绑定电脑

    using System.Management; namespace GLaLa { /// <summary> /// hardware_mac 的摘要说明. /// </summ ...

  7. java 获取本地 mac 地址

    主要参考:Java获取本机MAC地址/IP地址/主机名 做的更改: 1.我的windows是中文版,程序中获取mac时是按照physical address 获取的,添加上"物理地址&quo ...

  8. JAVA获取客户端IP地址和MAC地址

    1.获取客户端IP地址 public String getIp(HttpServletRequest request) throws Exception { String ip = request.g ...

  9. java 通过ip获取客户端mac地址

    java 通过ip获取客户端mac地址 package com.asppro.util; import java.io.BufferedReader; import java.io.IOExcepti ...

随机推荐

  1. Linux Awk使用案例总结

    知识点: 1)数组 数组是用来存储一系列值的变量,可通过索引来访问数组的值. Awk中数组称为关联数组,因为它的下标(索引)可以是数字也可以是字符串. 下标通常称为键,数组元素的键和值存储在Awk程序 ...

  2. 【收藏】实战Nginx与PHP(FastCGI)的安装、配置与优化

    拜读南非蚂蚁大牛的文章真是有所收获 http://ixdba.blog.51cto.com/2895551/806622 一.什么是 FastCGI FastCGI是一个可伸缩地.高速地在HTTP s ...

  3. PHP错误处理函数set_error_handler()的用法[转载]

    定义和用法 set_error_handler() 函数设置用户自定义的错误处理函数. 该函数用于创建运行时期间的用户自己的错误处理方法. 该函数会返回旧的错误处理程序,若失败,则返回 null. 语 ...

  4. File类的三种构造方法

    package cn.zmh.File; import java.io.File; /* * * File类的构造方法 三种重载形式 * * */ public class FileDemo1 { p ...

  5. MongoDB学习day07--mongoose入门,数据库增删改查,默认参数,模块化

    一.mongoose介绍 Mongoose 是在 node.js 异步环境下对 mongodb 进行便捷操作的对象模型工具. Mongoose 是 NodeJS 的驱动, 不能作为其他语言的驱动. M ...

  6. new String()理解

    public static void main(String[] args){ String a=new String("ddy"); String b=new String(&q ...

  7. Myeclipse配置jad

    下载地址:http://pan.baidu.com/s/1bnpMEuF 1.下载jad158g.win.zip 下载后解压.解压缩后将jad.exe拷贝到自定义的文件夹内:我这里用的是D:/jad/ ...

  8. OpenWRT解决因PPPOE丢包导致频繁掉线问题

    其关键在于这两个参数 lcp-echo-interval 1   #发送间隔秒 lcp-echo-failure 5    #5次未响应断开 因为OpenWRT默认的设置为1秒发送一次 5次没有响应就 ...

  9. Data obtained from ping: is it round trip or one way?

    I have 2 servers, each in two separate locations. I need to host an application on one, and the data ...

  10. C语言最小生成树prim算法(USACO3.1)

    /* ID: hk945801 LANG: C++ TASK: agrinet */ #include<iostream> #include<cstdio> using nam ...