|
|
@@ -0,0 +1,94 @@
|
|
|
+package com.fdkankan.ucenter.common;
|
|
|
+
|
|
|
+import cn.hutool.core.io.FileUtil;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.bouncycastle.crypto.io.MacInputStream;
|
|
|
+
|
|
|
+import java.io.BufferedReader;
|
|
|
+import java.io.File;
|
|
|
+import java.io.IOException;
|
|
|
+import java.io.InputStreamReader;
|
|
|
+import java.net.InetAddress;
|
|
|
+import java.net.NetworkInterface;
|
|
|
+import java.net.SocketException;
|
|
|
+import java.net.UnknownHostException;
|
|
|
+import java.util.Enumeration;
|
|
|
+
|
|
|
+@Slf4j
|
|
|
+public class GetLocalIp {
|
|
|
+
|
|
|
+ public static String ipAddress = null;
|
|
|
+
|
|
|
+ public static void main(String[] args) {
|
|
|
+ System.out.println(getIp());
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ public static String getIp(){
|
|
|
+ if(ipAddress != null){
|
|
|
+ return ipAddress;
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ Process process = Runtime.getRuntime().exec("ip addr show enp3s0 | grep 'inet ' | awk '{print $2}' | cut -d/ -f1");
|
|
|
+ BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
|
|
|
+ String line;
|
|
|
+ while ((line = bufferedReader.readLine()) != null) {
|
|
|
+ ipAddress += line;
|
|
|
+ log.info(line);
|
|
|
+ }
|
|
|
+ bufferedReader.close();
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.info("getIp-error:",e);
|
|
|
+ }
|
|
|
+ if(ipAddress == null){
|
|
|
+ return getIp3();
|
|
|
+ }
|
|
|
+ return ipAddress;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public static String getIp2(){
|
|
|
+ String os = System.getProperty("os.name").toLowerCase();
|
|
|
+ String ipAddress = null;
|
|
|
+ try {
|
|
|
+ if (os.contains("win")) {
|
|
|
+ Process process = Runtime.getRuntime().exec("ipconfig");
|
|
|
+ BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream(), "GBK"));
|
|
|
+ String line;
|
|
|
+ while ((line = bufferedReader.readLine()) != null) {
|
|
|
+ if (line.contains("IPv4") && !line.contains("IPv4地址")) {
|
|
|
+ ipAddress = line.substring(line.indexOf(":") + 2);
|
|
|
+ System.out.println("本机IP地址:" + ipAddress);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ bufferedReader.close();
|
|
|
+ } else {
|
|
|
+ Process process = Runtime.getRuntime().exec("ifconfig");
|
|
|
+ BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
|
|
|
+ String line;
|
|
|
+ while ((line = bufferedReader.readLine()) != null) {
|
|
|
+ if (line.contains("inet") && !line.contains("inet6")) {
|
|
|
+ ipAddress = line.substring(line.indexOf("inet") + 5, line.indexOf("netmask")).trim();
|
|
|
+ System.out.println("本机IP地址:" + ipAddress);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ bufferedReader.close();
|
|
|
+ }
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return ipAddress;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String getIp3(){
|
|
|
+ File file = new File("/mnt/shell/ip.txt");
|
|
|
+ if(!file.exists()){
|
|
|
+ log.info("/mnt/shell/ip.txt 不存在");
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ String result = FileUtil.readUtf8String(new File("/mnt/shell/ip.txt"));
|
|
|
+ ipAddress = result.replaceAll("[\n\r]", "");
|
|
|
+ return ipAddress;
|
|
|
+ }
|
|
|
+}
|