cm-cloud/cloud-security/src/main/java/com/cm/security/utils/AddressUtil.java

169 lines
4.7 KiB
Java
Raw Normal View History

2019-07-27 23:03:27 +08:00
package com.cm.security.utils;
import java.net.InetAddress;
import java.net.NetworkInterface;
2020-02-17 23:21:23 +08:00
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
2019-07-27 23:03:27 +08:00
public class AddressUtil {
/**
2020-02-17 23:21:23 +08:00
* 获取MAC地址, 可能会为空
2019-07-27 23:03:27 +08:00
*
* @return
* @throws Exception
*/
2020-02-17 23:21:23 +08:00
@Deprecated
2019-07-27 23:03:27 +08:00
public static final String getMacAddress() throws Exception {
InetAddress inetAddress = InetAddress.getLocalHost();
byte[] mac = NetworkInterface.getByInetAddress(inetAddress).getHardwareAddress();
StringBuilder macAddress = new StringBuilder();
for (byte macByte : mac) {
if (macAddress.length() > 0) {
macAddress.append("-");
}
int macInt = macByte & 0xff;
String str = Integer.toHexString(macInt);
macAddress.append(str.length() == 1 ? 0 + str : str);
}
return macAddress.toString().toUpperCase();
}
2020-02-17 23:21:23 +08:00
/**
* 此方法描述的是获得服务器的IP地址
*/
public static String getLocalIP() throws Exception {
String sIP = "";
InetAddress ip = null;
boolean bFindIP = false;
Enumeration<NetworkInterface> netInterfaces = (Enumeration<NetworkInterface>) NetworkInterface
.getNetworkInterfaces();
while (netInterfaces.hasMoreElements()) {
if (bFindIP) {
break;
}
NetworkInterface ni = (NetworkInterface) netInterfaces
.nextElement();
Enumeration<InetAddress> ips = ni.getInetAddresses();
while (ips.hasMoreElements()) {
ip = (InetAddress) ips.nextElement();
if (!ip.isLoopbackAddress()
&& ip.getHostAddress().matches(
"(\\d{1,3}\\.){3}\\d{1,3}")) {
bFindIP = true;
break;
}
}
}
if (null != ip) {
sIP = ip.getHostAddress();
}
return sIP;
}
/**
* 此方法描述的是获得服务器的IP地址(多网卡)
*/
public static List<String> getLocalIPS() throws Exception {
InetAddress ip = null;
List<String> ipList = new ArrayList<String>();
Enumeration<NetworkInterface> netInterfaces = (Enumeration<NetworkInterface>) NetworkInterface
.getNetworkInterfaces();
while (netInterfaces.hasMoreElements()) {
NetworkInterface ni = (NetworkInterface) netInterfaces
.nextElement();
Enumeration<InetAddress> ips = ni.getInetAddresses();
while (ips.hasMoreElements()) {
ip = (InetAddress) ips.nextElement();
if (!ip.isLoopbackAddress()
&& ip.getHostAddress().matches(
"(\\d{1,3}\\.){3}\\d{1,3}")) {
ipList.add(ip.getHostAddress());
}
}
}
return ipList;
}
/**
* 获得服务器的MAC地址
*/
public static String getMacId() throws Exception {
String macId = "";
InetAddress ip = null;
NetworkInterface ni = null;
boolean bFindIP = false;
Enumeration<NetworkInterface> netInterfaces = (Enumeration<NetworkInterface>) NetworkInterface
.getNetworkInterfaces();
while (netInterfaces.hasMoreElements()) {
if (bFindIP) {
break;
}
ni = (NetworkInterface) netInterfaces
.nextElement();
Enumeration<InetAddress> ips = ni.getInetAddresses();
while (ips.hasMoreElements()) {
ip = (InetAddress) ips.nextElement();
// 非127.0.0.1
if (!ip.isLoopbackAddress()
&& ip.getHostAddress().matches(
"(\\d{1,3}\\.){3}\\d{1,3}")) {
bFindIP = true;
break;
}
}
}
if (null != ip) {
macId = getMacFromBytes(ni.getHardwareAddress());
}
return macId;
}
/**
* 获得服务器的MAC地址(多网卡)
*/
public static List<String> getMacIds() throws Exception {
InetAddress ip = null;
NetworkInterface ni = null;
List<String> macList = new ArrayList<String>();
Enumeration<NetworkInterface> netInterfaces = (Enumeration<NetworkInterface>) NetworkInterface
.getNetworkInterfaces();
while (netInterfaces.hasMoreElements()) {
ni = (NetworkInterface) netInterfaces
.nextElement();
// ----------特定情况可以考虑用ni.getName判断
// 遍历所有ip
Enumeration<InetAddress> 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()));
}
}
}
return macList;
}
private static String getMacFromBytes(byte[] bytes) {
StringBuffer mac = new StringBuffer();
byte currentByte;
boolean first = false;
for (byte b : 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;
}
return mac.toString().toUpperCase();
}
2019-07-27 23:03:27 +08:00
}