All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.leopard.commons.utility.ServerIpUtil Maven / Gradle / Ivy

There is a newer version: 0.9.12
Show newest version
package io.leopard.commons.utility;

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.SystemUtils;

public class ServerIpUtil {
	private static String hostAddress = null;

	public static String getServerIp() {
		if (hostAddress != null) {
			return hostAddress;
		}
		hostAddress = getServerIpNoCache();
		return hostAddress;
	}

	private static String getServerIpNoCache() {
		if (SystemUtils.IS_OS_WINDOWS) {
			return "127.0.0.1";
		}
		else if (SystemUtils.IS_OS_MAC) {
			return "127.0.0.1";
		}
		else if (SystemUtils.IS_OS_LINUX) {
			try {
				return getServerIp("eth0");
			}
			catch (SocketException e) {
				throw new RuntimeException(e.getMessage(), e);
			}
		}
		else {
			throw new RuntimeException("未知操作系统.");
		}

	}

	public static String getServerIp(String displayName) throws SocketException {
		NetworkInterface networkInterface = NetworkInterface.getByName(displayName);
		List subIpList = listSubIp(networkInterface);
		List ipList = listAllIp(networkInterface);
		// System.out.println("ipList:" + ipList);
		// System.out.println("subIpList:" + subIpList);
		ipList.removeAll(subIpList);
		if (ipList.size() == 0) {
			throw new RuntimeException("为什么IP列表为空?]");
		}
		else if (ipList.size() > 1) {
			throw new RuntimeException("怎么IP数量超过1个?[" + StringUtils.join(ipList, ",") + "]");
		}
		String serverIp = ipList.get(0);
		// System.out.println("serverIp:" + serverIp);
		return serverIp;
	}

	private static List listSubIp(NetworkInterface networkInterface) {
		List subIpList = new ArrayList();
		Enumeration subInterfaces = networkInterface.getSubInterfaces();
		while (subInterfaces.hasMoreElements()) {
			NetworkInterface net = subInterfaces.nextElement();
			List ipList = listAllIp(net);
			String name = net.getName();
			System.out.println("name:" + name + " ipList:" + ipList);
			subIpList.addAll(ipList);
		}
		return subIpList;
	}

	private static List listAllIp(NetworkInterface networkInterface) {
		List ipList = new ArrayList();
		Enumeration ias = networkInterface.getInetAddresses();
		while (ias.hasMoreElements()) {
			InetAddress inet = ias.nextElement();
			String ip = inet.getHostAddress();
			if (ip.indexOf(".") == -1) {
				// ipv6
			}
			else {
				ipList.add(ip);
			}
		}
		return ipList;
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy