mirror of
https://github.com/alibaba/arthas.git
synced 2026-11-30 15:33:59 +00:00
IPUtils.java: comments and some minimum refactor
This commit is contained in:
1 file changed
+19
-28
@@ -5,55 +5,52 @@ import java.net.NetworkInterface;
|
|||||||
import java.util.Enumeration;
|
import java.util.Enumeration;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author weipeng2k 2015年1月30日 下午3:06:47
|
* @author weipeng2k 2015-01-30 15:06:47
|
||||||
*/
|
*/
|
||||||
public class IPUtils {
|
public class IPUtils {
|
||||||
|
|
||||||
|
private static final String WINDOWS = "windows";
|
||||||
|
private static final String OS_NAME = "os.name";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 判断当前操作是否Windows.
|
* check: whether current operating system is windows
|
||||||
*
|
*
|
||||||
* @return true---是Windows操作系统
|
* @return true---is windows
|
||||||
*/
|
*/
|
||||||
public static boolean isWindowsOS() {
|
public static boolean isWindowsOS() {
|
||||||
boolean isWindowsOS = false;
|
String osName = System.getProperty(OS_NAME);
|
||||||
String osName = System.getProperty("os.name");
|
return osName.toLowerCase().contains(WINDOWS);
|
||||||
if (osName.toLowerCase().indexOf("windows") > -1) {
|
|
||||||
isWindowsOS = true;
|
|
||||||
}
|
|
||||||
return isWindowsOS;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取本机IP地址,并自动区分Windows还是Linux操作系统
|
* get IP address, automatically distinguish the operating system.(windows or linux)
|
||||||
*
|
*
|
||||||
* @return String
|
* @return String
|
||||||
*/
|
*/
|
||||||
public static String getLocalIP() {
|
public static String getLocalIP() {
|
||||||
String sIP = null;
|
|
||||||
InetAddress ip = null;
|
InetAddress ip = null;
|
||||||
try {
|
try {
|
||||||
if (isWindowsOS()) {
|
if (isWindowsOS()) {
|
||||||
ip = InetAddress.getLocalHost();
|
ip = InetAddress.getLocalHost();
|
||||||
} else {
|
} else {
|
||||||
// 如果是回环地址则扫描所有的NetWorkInterface
|
//scan all NetWorkInterfaces if it's loopback address
|
||||||
if (!InetAddress.getLocalHost().isLoopbackAddress()) {
|
if (!InetAddress.getLocalHost().isLoopbackAddress()) {
|
||||||
ip = InetAddress.getLocalHost();
|
ip = InetAddress.getLocalHost();
|
||||||
} else {
|
} else {
|
||||||
boolean bFindIP = false;
|
boolean bFindIP = false;
|
||||||
Enumeration<NetworkInterface> netInterfaces = (Enumeration<NetworkInterface>) NetworkInterface.getNetworkInterfaces();
|
Enumeration<NetworkInterface> netInterfaces = NetworkInterface.getNetworkInterfaces();
|
||||||
while (netInterfaces.hasMoreElements()) {
|
while (netInterfaces.hasMoreElements()) {
|
||||||
if (bFindIP) {
|
if (bFindIP) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
NetworkInterface ni = (NetworkInterface) netInterfaces.nextElement();
|
NetworkInterface ni = netInterfaces.nextElement();
|
||||||
// ----------特定情况,可以考虑用ni.getName判断
|
// ----------特定情况,可以考虑用ni.getName判断
|
||||||
// 遍历所有ip
|
// iterator all IPs
|
||||||
Enumeration<InetAddress> ips = ni.getInetAddresses();
|
Enumeration<InetAddress> ips = ni.getInetAddresses();
|
||||||
while (ips.hasMoreElements()) {
|
while (ips.hasMoreElements()) {
|
||||||
ip = (InetAddress) ips.nextElement();
|
ip = ips.nextElement();
|
||||||
// 127.开头的都是lookback地址
|
// IP starts with 127. is loopback address
|
||||||
if (ip.isSiteLocalAddress() && !ip.isLoopbackAddress()
|
if (ip.isSiteLocalAddress() && !ip.isLoopbackAddress() && !ip.getHostAddress().contains(":")) {
|
||||||
&& ip.getHostAddress().indexOf(":") == -1) {
|
|
||||||
bFindIP = true;
|
bFindIP = true;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@@ -65,13 +62,7 @@ public class IPUtils {
|
|||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
}
|
}
|
||||||
|
|
||||||
if (ip != null) {
|
return ip == null ? null : ip.getHostAddress();
|
||||||
sIP = ip.getHostAddress();
|
|
||||||
}
|
|
||||||
return sIP;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void main(String[] args) {
|
|
||||||
System.out.println(getLocalIP());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user