Skip to content

Commit 2fcc919

Browse files
authored
fix: check localhost ip first on fallback (#4158)
1 parent 75852ab commit 2fcc919

File tree

1 file changed

+18
-13
lines changed

1 file changed

+18
-13
lines changed

lgsm/functions/check_ip.sh

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -26,32 +26,37 @@ for ethtool_command in "${ethtool_commands_array[@]}"; do
2626
fi
2727
done
2828

29-
getip=$(${ipcommand} -o -4 addr | awk '{print $4}' | grep -oe '\([0-9]\{1,3\}\.\?\)\{4\}' | sort -u | grep -v 127.0.0)
30-
getipwc=$(${ipcommand} -o -4 addr | awk '{print $4}' | grep -oe '\([0-9]\{1,3\}\.\?\)\{4\}' | sort -u | grep -vc 127.0.0)
29+
mapfile -t current_ips < <(${ipcommand} -o -4 addr | awk '{print $4}' | grep -oe '\([0-9]\{1,3\}\.\?\)\{4\}' | sort -u | grep -v 127.0.0)
30+
31+
function fn_is_valid_ip() {
32+
local ip="${1}"
33+
# excluding 0.* ips also
34+
grep -qEe '^[1-9]+[0-9]*\.[0-9]+\.[0-9]+\.[0-9]+$' <<< "${ip}"
35+
}
3136

3237
# Check if server has multiple IP addresses
3338

3439
# If the IP variable has been set by user.
35-
if [ -n "${ip}" ] && [ "${ip}" != "0.0.0.0" ]; then
36-
queryips=("${ip}")
37-
webadminip=("${ip}")
38-
telnetip=("${ip}")
40+
if fn_is_valid_ip "${ip}"; then
41+
queryips=( "${ip}" )
42+
webadminip=( "${ip}" )
43+
telnetip=( "${ip}" )
3944
# If game config does have an IP set.
40-
elif [ -n "${configip}" ] && [ "${configip}" != "0.0.0.0" ]; then
41-
queryips=("${configip}")
45+
elif fn_is_valid_ip "${configip}";then
46+
queryips=( "${configip}" )
4247
ip="${configip}"
4348
webadminip=("${configip}")
4449
telnetip=("${configip}")
4550
# If there is only 1 server IP address.
4651
# Some IP details can automaticly use the one IP
47-
elif [ "${getipwc}" == "1" ]; then
48-
queryips=($(echo "${getip}"))
52+
elif [ "${#current_ips[@]}" == "1" ]; then
53+
queryips=( "127.0.0.1" "${current_ips[@]}" )
4954
ip="0.0.0.0"
50-
webadminip=("${getip}")
51-
telnetip=("${getip}")
55+
webadminip=("${current_ips[@]}")
56+
telnetip=("${current_ips[@]}")
5257
# If no ip is set by the user and server has more than one IP.
5358
else
54-
queryips=($(echo "${getip}"))
59+
queryips=( "127.0.0.1" "${current_ips[@]}" )
5560
ip="0.0.0.0"
5661
webadminip=("${ip}")
5762
telnetip=("${ip}")

0 commit comments

Comments
 (0)