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. SSH日志位置

    # Redhat or Fedora Core: /var/log/secure # Mandrake, FreeBSD or OpenBSD: /var/log/auth.log # SuSE: / ...

  2. SOJ 4482 忽悠大神【最小生成树】

    题目链接: http://acm.scu.edu.cn/soj/problem.action?id=4482 题意: 给定边权和点权,从一个点出发并回到该点,减少尽量多的边,每路过点和边都要把权重加到 ...

  3. PAT (Advanced Level) 1034. Head of a Gang (30)

    简单DFS. #include<cstdio> #include<cstring> #include<cmath> #include<vector> # ...

  4. [bzoj2595][WC2008]游览计划/[bzoj5180][Baltic2016]Cities_斯坦纳树

    游览计划 bzoj-2595 wc-2008 题目大意:题目链接.题目连接. 注释:略. 想法:裸题求斯坦纳树. 斯坦纳树有两种转移方式,设$f[s][i]$表示联通状态为$s$,以$i$为根的最小代 ...

  5. Go---Redis连接池

    之前一篇文章介绍过使用redigo连接redis数据库处理,在使用中发现如果初始化一条链接连接redis做相关操作,使用中发现当两个程序交替使用redis时,先前建立的链接会断掉,只能每次操作的时候重 ...

  6. kvm虚拟化学习笔记(三)之windows kvm虚拟机安装

    KVM虚拟化学习笔记系列文章列表----------------------------------------kvm虚拟化学习笔记(一)之kvm虚拟化环境安装http://koumm.blog.51 ...

  7. 利用NSA的MS17-010漏洞利用工具实现Win 7和Win Server 2008系统入侵

    影子经纪人(Shadow Brokers)最近陆续曝光的NSA网络武器令人震惊,尽管这些工具是否出自国家级别黑客团队之手尚不清楚,但至少存在一个可以说明问题的事实:这些漏洞利用工具都能有效运行,且具有 ...

  8. Python访问MySQL数据库并实现其增删改查功能

    概述:对于访问MySQL数据库的操作,我想大家也都有一些了解.不过,因为最近在学习Python,以下就用Python来实现它.其中包括创建数据库和数据表.插入记录.删除记录.修改记录数据.查询数据.删 ...

  9. Antivius for Linux

    http://www.clamav.net/   https://www.avast.com/zh-cn/linux-server-antivirus   http://www.f-prot.com/ ...

  10. 16款创建CSS3动画的jQuery插件

    jQuery插件是用来扩展jQuery原型对象的方法. 本文搜集了用来为你的站点创建CSS3动画的一些jQuery插件. 1. jQuery Smoove Smoove 简化了CSS3转换效果.使得页 ...