From: Nicolas Boisselier Date: Thu, 27 Jun 2019 14:16:30 +0000 (+0100) Subject: etc/profile.d/net.sh X-Git-Url: https://git.nbdom.net/?a=commitdiff_plain;h=45727a973e8f5843b88fdae6eb6eab870c344418;p=nb.git etc/profile.d/net.sh --- diff --git a/etc/profile.d/functions b/etc/profile.d/functions index 0e0052fa..d8978fc2 100755 --- a/etc/profile.d/functions +++ b/etc/profile.d/functions @@ -932,14 +932,3 @@ cron_d_install() { [ "$cron" != "$(crontab -l)" ] && echo "$cron" | crontab } - -ip_is_local() { - case "$1" in - 10.*) return 0 ;; - 172.16.*) return 0 ;; - 172.2[0-9].*) return 0 ;; - 172.31.*) return 0 ;; - 192.168.*) return 0 ;; - esac - return 1 -} diff --git a/etc/profile.d/net.sh b/etc/profile.d/net.sh index c0ab149c..cedcff9c 100644 --- a/etc/profile.d/net.sh +++ b/etc/profile.d/net.sh @@ -49,3 +49,26 @@ net_mask2cdr() { x=${1%%$3*} echo $(( $2 + (${#x}/4) )) } + +net_host_ip() { + host -s "$1" | awk '/has address/{print $4; exit}' +} + +net_is_local() { + declare ip="${1:?Usage: net_is_local IP|HOST}" + case "$ip" in + [0-9][0-9]?[0-9]?.[0-9][0-9]?[0-9]?.[0-9][0-9]?[0-9]?.[0-9][0-9]?[0-9]?) [ 1 ];; + *) ip="$(net_host_ip "$ip")" + esac + + [ -n "$ip" ] || return 2 + + case "$ip" in + 10.*) return 0 ;; + 172.16.*) return 0 ;; + 172.2[0-9].*) return 0 ;; + 172.31.*) return 0 ;; + 192.168.*) return 0 ;; + *) return 1 ;; + esac +}