From cce2d05950559367dbe17d4f6ca1819588b0e3b7 Mon Sep 17 00:00:00 2001 From: Vlastimil Holer Date: Fri, 1 Dec 2017 18:51:37 +0100 Subject: [PATCH] Improve get_first_ip() for systems w/o default route and new iproute - Current ip returns user UID for the route (Fedora 25, 26): 1.0.0.0 via 10.0.0.2 dev eth0 src 10.10.0.43 uid 0 cache - There may be more spaces between device and src (Ubuntu 14.04): 1.0.0.0 via 10.0.0.2 dev eth0 src 10.10.0.37 cache --- src/etc/one-context.d/net-15-hostname | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/etc/one-context.d/net-15-hostname b/src/etc/one-context.d/net-15-hostname index b1e4a81..d70325b 100755 --- a/src/etc/one-context.d/net-15-hostname +++ b/src/etc/one-context.d/net-15-hostname @@ -24,7 +24,14 @@ function set_domainname() { } function get_first_ip() { - ip route get 1 | awk '{print $NF;exit}' + local ip + + ip=${ip:-$(ip route get 1 2>/dev/null | grep 'src [0-9\.]\+' | head -1 | sed -e 's/^.*src \([0-9\.]*\).*$/\1/')} + ip=${ip:-$(ip -4 address show scope global up 2>/dev/null | awk '/inet / { gsub(/\/[^\/]+$/, "", $2); print $2; exit}')} + ip=${ip:-$(hostname -I 2>/dev/null | cut -d' ' -f1)} + ip=${ip:-$(hostname -i 2>/dev/null)} + + echo "${ip}" } function get_dns_name() {