From ac8ca3157e8b6156ef3df585f46c9a163e217063 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=AE=8B=E5=BA=B7?= Date: Tue, 24 Sep 2024 18:23:57 +0800 Subject: [PATCH] OKG Network Information Getting Shell Script Update MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 宋康 --- .../current/user-manuals/network.md | 27 +++++++++++++++++++ kruisegame/user-manuals/network.md | 27 +++++++++++++++++++ 2 files changed, 54 insertions(+) diff --git a/i18n/zh/docusaurus-plugin-content-docs-kruisegame/current/user-manuals/network.md b/i18n/zh/docusaurus-plugin-content-docs-kruisegame/current/user-manuals/network.md index 2cdcde649..d6547af61 100644 --- a/i18n/zh/docusaurus-plugin-content-docs-kruisegame/current/user-manuals/network.md +++ b/i18n/zh/docusaurus-plugin-content-docs-kruisegame/current/user-manuals/network.md @@ -1507,6 +1507,33 @@ func getNetwork() { ``` +在获取网络信息时,可能面临着网络状态未准备好的情况,可以在程序中加入一个循环,持续检测网络状态,直至网络状态为Ready后获取网络信息数据。以下是相应的shell脚本。 + +```shell +# 初始化JSON字符串为空 +json='' + +# 循环直到拿到数据 +while [ -z "$json" ]; do + # 从文件中读取JSON字符串 + json=$(cat /etc/podinfo/network) + + # 检查currentNetworkState是否为Ready + currentNetworkState=$(echo $json | jq -r '.currentNetworkState') + if [ "$currentNetworkState" != "Ready" ]; then + echo "currentNetworkState is not Ready, sleeping 1 second..." + sleep 1 + json='' + fi +done + +# 解析externalAddresses的ip和port +echo "externalAddresses:" +ip=$(echo $json | jq -r '.externalAddresses[0].ip') +port=$(echo $json | jq -r '.externalAddresses[0].ports[0].port') +echo " IP: $ip, Port: $port" +``` + ## FAQ Q: 如何更改网络插件配置? diff --git a/kruisegame/user-manuals/network.md b/kruisegame/user-manuals/network.md index 441930e74..984391e41 100644 --- a/kruisegame/user-manuals/network.md +++ b/kruisegame/user-manuals/network.md @@ -1477,6 +1477,33 @@ func getNetwork() { ``` +When getting network information, you may face the situation that the network status is not ready, you can add a loop in the programme to continuously detect the network status until the network status is Ready and then get the network information data. The following is the corresponding shell script. + +```shell +# Initialise JSON string to empty +json='' + +# Loop until you get the data +while [ -z "$json" ]; do + # Reading a JSON string from a file + json=$(cat /etc/podinfo/network) + + # Check if currentNetworkState is Ready + currentNetworkState=$(echo $json | jq -r '.currentNetworkState') + if [ "$currentNetworkState" != "Ready" ]; then + echo "currentNetworkState is not Ready, sleeping 1 second..." + sleep 1 + json='' + fi +done + +# Parsing the ip and port of externalAddresses +echo "externalAddresses:" +ip=$(echo $json | jq -r '.externalAddresses[0].ip') +port=$(echo $json | jq -r '.externalAddresses[0].ports[0].port') +echo " IP: $ip, Port: $port" +``` + ## FAQ Q: How to change the network plugin configuration?