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
pull/100/head
Vlastimil Holer 7 years ago
parent 2dc909a469
commit cce2d05950
No known key found for this signature in database
GPG Key ID: EB549BADEFD07C53

@ -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() {

Loading…
Cancel
Save