Prioritize eth0 IP lookup in get_lxc_ip function

The get_lxc_ip function now attempts to retrieve the IP address directly from eth0 before falling back to hostname -I. This improves reliability for LXC containers where eth0 is typically the primary interface.
This commit is contained in:
CanbiZ (MickLesk)
2026-01-19 17:41:17 +01:00
parent 008f6af0bd
commit de20a41423

View File

@@ -898,7 +898,14 @@ function get_lxc_ip() {
get_current_ip() {
local ip
# Try hostname -I first (most reliable for LXC containers)
# Try direct interface lookup for eth0 FIRST (most reliable for LXC)
ip=$(ip -4 addr show eth0 2>/dev/null | awk '/inet / {print $2}' | cut -d/ -f1 | head -n1)
if [[ -n "$ip" && "$ip" =~ ^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
echo "$ip"
return 0
fi
# Fallback: Try hostname -I
if command -v hostname >/dev/null 2>&1; then
ip=$(hostname -I 2>/dev/null | awk '{print $1}')
if [[ -n "$ip" && "$ip" =~ ^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
@@ -907,13 +914,6 @@ function get_lxc_ip() {
fi
fi
# Fallback: Try direct interface lookup for eth0
ip=$(ip -4 addr show eth0 2>/dev/null | awk '/inet / {print $2}' | cut -d/ -f1 | head -n1)
if [[ -n "$ip" ]]; then
echo "$ip"
return 0
fi
# Last resort: Use routing table
local targets=("8.8.8.8" "1.1.1.1" "default")
for target in "${targets[@]}"; do