java获取机器序列号_Java获取主板序列号、MAC地址、CPU序列号工具类

这个Java工具类提供了获取操作系统名称、Linux和Windows环境下网卡MAC地址的方法。通过执行不同系统的命令来获取MAC地址,适用于非Windows和Windows系统。在Windows下遍历所有网络接口,过滤掉loopback地址,获取第一个符合条件的MAC地址。
摘要由CSDN通过智能技术生成

importjava.io.BufferedReader;importjava.io.IOException;importjava.io.InputStreamReader;importjava.net.InetAddress;importjava.net.NetworkInterface;importjava.util.ArrayList;importjava.util.Enumeration;importjava.util.List;importcom.tjhq.hqoa.utils.Log4jUtil;importcom.tjhq.hqoa.utils.StringUtil;/*** 与系统相关的一些常用工具方法.

*

*@version1.0.0*/

public classMACUtil {/*** 获取当前操作系统名称. return 操作系统名称 例如:windows xp,linux 等.*/

public staticString getOSName() {return System.getProperty("os.name").toLowerCase();

}/*** 获取unix网卡的mac地址. 非windows的系统默认调用本方法获取. 如果有特殊系统请继续扩充新的取mac地址方法.

*

*@returnmac地址*/

public staticString getMAC_linux() {

String mac= null;

BufferedReader bufferedReader= null;

Process process= null;try{//linux下的命令,一般取eth0作为本地主网卡

process = Runtime.getRuntime().exec("ifconfig eth0");//显示信息中包含有mac地址信息

bufferedReader = new BufferedReader(newInputStreamReader(

process.getInputStream()));

String line= null;int index = -1;while ((line = bufferedReader.readLine()) != null) {//寻找标示字符串[hwaddr]

index = line.toLowerCase().indexOf("hwaddr");if (index >= 0) {//找到了//取出mac地址并去除2边空格

mac = line.substring(index + "hwaddr".length() + 1).trim();break;

}

}

}catch(IOException e) {

Log4jUtil.error("获取mac信息错误",e);

}finally{try{if (bufferedReader != null) {

bufferedReader.close();

}

}catch(IOException e1) {

Log4jUtil.error("获取mac信息错误",e1);

}

bufferedReader= null;

process= null;

}returnmac;

}/*** 获取widnows网卡的mac地址.

*

*@returnmac地址*/

public staticString getMAC_windows() {

InetAddress ip= null;

NetworkInterface ni= null;

List macList = new ArrayList();try{

Enumeration netInterfaces = (Enumeration) NetworkInterface

.getNetworkInterfaces();while(netInterfaces.hasMoreElements()) {

ni=(NetworkInterface) netInterfaces.nextElement();//----------特定情况,可以考虑用ni.getName判断//遍历所有ip

Enumeration ips =ni.getInetAddresses();while(ips.hasMoreElements()) {

ip=(InetAddress) ips.nextElement();if (!ip.isLoopbackAddress() //非127.0.0.1

&&ip.getHostAddress().matches("(\\d{1,3}\\.){3}\\d{1,3}")) {

macList.add(getMacFromBytes(ni.getHardwareAddress()));

}

}

}

}catch(Exception e) {

Log4jUtil.error("获取mac错误", e);

}if (macList.size() > 0) {return macList.get(0);

}else{return "";

}

}private static String getMacFromBytes(byte[] bytes) {

StringBuffer mac= newStringBuffer();bytecurrentByte;boolean first = false;for (byteb : bytes) {if(first) {

mac.append("-");

}

currentByte= (byte) ((b & 240) >> 4);

mac.append(Integer.toHexString(currentByte));

currentByte= (byte) (b & 15);

mac.append(Integer.toHexString(currentByte));

first= true;

}returnmac.toString().toUpperCase();

}public static String getMAC() throwsException {

String os=getOSName();

String mac= "";if (os.startsWith("windows")) {

mac=getMAC_windows();

}else if (os.startsWith("linux")) {

mac=getMAC_linux();

}if(!StringUtil.isNotNullOrBlank(mac)){

mac="null";

}returnmac;

}/*** 测试用的main方法.

*

*@paramargc

* 运行参数.

*@throwsException*/

public static void main(String[] argc) throwsException {

String mac=getMAC();

System.out.println(mac);

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值