From 6f5ea9eead786014ae3d05b529bbe09915a2a879 Mon Sep 17 00:00:00 2001 From: xuelianhan007 Date: Fri, 29 Nov 2024 11:45:47 +0800 Subject: [PATCH] fix fqdn name query on ECS --- .../kraken/operator/core/toolkit/IpUtils.java | 17 ++++++++++++++--- .../kraken/operator/core/IpUtilsTest.java | 6 ++++++ 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/kraken-java-sdk/kraken-java-sdk-core/src/main/java/com/consoleconnect/kraken/operator/core/toolkit/IpUtils.java b/kraken-java-sdk/kraken-java-sdk-core/src/main/java/com/consoleconnect/kraken/operator/core/toolkit/IpUtils.java index be64aa6c..86b69373 100644 --- a/kraken-java-sdk/kraken-java-sdk-core/src/main/java/com/consoleconnect/kraken/operator/core/toolkit/IpUtils.java +++ b/kraken-java-sdk/kraken-java-sdk-core/src/main/java/com/consoleconnect/kraken/operator/core/toolkit/IpUtils.java @@ -1,5 +1,8 @@ package com.consoleconnect.kraken.operator.core.toolkit; +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; import java.net.InetAddress; import java.net.InetSocketAddress; import java.net.UnknownHostException; @@ -53,9 +56,17 @@ public static String getHostAddress() { public static String getFQDN() { try { - InetAddress address = InetAddress.getLocalHost(); - return address.getCanonicalHostName(); - } catch (UnknownHostException e) { + ProcessBuilder builder = new ProcessBuilder(); + builder.command("bash", "-c", "hostname -f"); + Process process = builder.start(); + BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream())); + String line; + StringBuilder result = new StringBuilder(); + while ((line = reader.readLine()) != null) { + result.append(line); + } + return result.toString(); + } catch (IOException e) { return IP_UNKNOWN; } } diff --git a/kraken-java-sdk/kraken-java-sdk-core/src/test/java/com/consoleconnect/kraken/operator/core/IpUtilsTest.java b/kraken-java-sdk/kraken-java-sdk-core/src/test/java/com/consoleconnect/kraken/operator/core/IpUtilsTest.java index bd7e8675..e1ede6b7 100644 --- a/kraken-java-sdk/kraken-java-sdk-core/src/test/java/com/consoleconnect/kraken/operator/core/IpUtilsTest.java +++ b/kraken-java-sdk/kraken-java-sdk-core/src/test/java/com/consoleconnect/kraken/operator/core/IpUtilsTest.java @@ -16,4 +16,10 @@ void givenRequest_whenGetIpAddress_thenReturnIpAddress() { String ip = IpUtils.getIP(request); Assertions.assertNotNull(ip); } + + @Test + void givenECS_whenQueryFQDN_thenReturnSuccess() { + String result = IpUtils.getFQDN(); + Assertions.assertNotEquals("unknown", result); + } }