153 lines
4.2 KiB
Java
153 lines
4.2 KiB
Java
package com.zhgd.xmgl.util;
|
|
|
|
import cn.hutool.crypto.SecureUtil;
|
|
|
|
import java.io.BufferedReader;
|
|
import java.io.IOException;
|
|
import java.io.InputStreamReader;
|
|
import java.net.InetAddress;
|
|
import java.util.ArrayList;
|
|
import java.util.Iterator;
|
|
import java.util.List;
|
|
import java.util.regex.Matcher;
|
|
import java.util.regex.Pattern;
|
|
|
|
/**
|
|
* @program: wisdomSite
|
|
* @description: 获取当前电脑MAC地址或IP工具类
|
|
* @author: Mr.Peng
|
|
* @create: 2021-04-23 15:36
|
|
**/
|
|
|
|
public abstract class ComputerInfo {
|
|
//private static String macAddressStr = null;
|
|
private static String computerName = System.getenv().get("COMPUTERNAME");
|
|
|
|
private static final String[] windowsCommand = { "ipconfig", "/all" };
|
|
private static final String[] linuxCommand = { "/sbin/ifconfig", "-a" };
|
|
private static final Pattern macPattern = Pattern.compile(".*((:?[0-9a-f]{2}[-:]){5}[0-9a-f]{2}).*",
|
|
Pattern.CASE_INSENSITIVE);
|
|
|
|
/**
|
|
* 获取多个网卡地址
|
|
*
|
|
* @return
|
|
* @throws IOException
|
|
*/
|
|
private final static List<String> getMacAddressList() throws IOException {
|
|
final ArrayList<String> macAddressList = new ArrayList<String>();
|
|
final String os = System.getProperty("os.name");
|
|
final String command[];
|
|
|
|
if (os.startsWith("Windows")) {
|
|
command = windowsCommand;
|
|
} else if (os.startsWith("Linux")) {
|
|
command = linuxCommand;
|
|
} else {
|
|
throw new IOException("Unknow operating system:" + os);
|
|
}
|
|
// 执行命令
|
|
final Process process = Runtime.getRuntime().exec(command);
|
|
|
|
BufferedReader bufReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
|
|
for (String line = null; (line = bufReader.readLine()) != null;) {
|
|
Matcher matcher = macPattern.matcher(line);
|
|
if (matcher.matches()) {
|
|
macAddressList.add(matcher.group(1));
|
|
// macAddressList.add(matcher.group(1).replaceAll("[-:]",
|
|
// ""));//去掉MAC中的“-”
|
|
}
|
|
}
|
|
|
|
process.destroy();
|
|
bufReader.close();
|
|
return macAddressList;
|
|
}
|
|
|
|
/**
|
|
* 获取一个网卡地址(多个网卡时从中获取一个)
|
|
*
|
|
* @return
|
|
*/
|
|
public static List<String> getMacAddress() {
|
|
List<String> list=new ArrayList<>();
|
|
try {
|
|
List<String> macList = getMacAddressList();
|
|
for (Iterator<String> iter = macList.iterator(); iter.hasNext();) {
|
|
String amac = iter.next();
|
|
if (!amac.equals("0000000000E0")) {
|
|
list.add(amac);
|
|
}
|
|
}
|
|
} catch (IOException e) {
|
|
e.printStackTrace();
|
|
}
|
|
return list;
|
|
}
|
|
|
|
/**
|
|
* 对获取的网卡地址进行MD5加密
|
|
*
|
|
* @return
|
|
*/
|
|
public static List<String> getMd5MacAddress() {
|
|
List<String> list=getMacAddress();
|
|
List<String> tempList=new ArrayList<>();
|
|
if(list.size()>0){
|
|
for(String str:list){
|
|
tempList.add(SecureUtil.md5(str));
|
|
}
|
|
}
|
|
return tempList;
|
|
}
|
|
|
|
|
|
/**
|
|
* 获取电脑名
|
|
*
|
|
* @return
|
|
*/
|
|
public static String getComputerName() {
|
|
if (computerName == null || computerName.equals("")) {
|
|
computerName = System.getenv().get("COMPUTERNAME");
|
|
}
|
|
return computerName;
|
|
}
|
|
|
|
/**
|
|
* 获取客户端IP地址
|
|
*
|
|
* @return
|
|
*/
|
|
public static String getIpAddrAndName() throws IOException {
|
|
return InetAddress.getLocalHost().toString();
|
|
}
|
|
|
|
/**
|
|
* 获取客户端IP地址
|
|
*
|
|
* @return
|
|
*/
|
|
public static String getIpAddr() throws IOException {
|
|
return InetAddress.getLocalHost().getHostAddress().toString();
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
* 限制创建实例
|
|
*/
|
|
private ComputerInfo() {
|
|
|
|
}
|
|
|
|
public static void main(String[] args) throws IOException {
|
|
//log.info(ComputerInfo.getMacAddress());
|
|
//log.info(ComputerInfo.getComputerName());
|
|
//log.info(ComputerInfo.getIpAddr());
|
|
//log.info(ComputerInfo.getIpAddrAndName());
|
|
System.out.println(SecureUtil.md5("30-9C-23-20-0B-05"));
|
|
}
|
|
|
|
}
|