network/net-lib.sh: add is_ip()

add function to test if string is a valid IP

(cherry picked from commit 01b23b6900)
Harald Hoyer 2015-08-12 14:24:05 +02:00
parent fc0ead21c9
commit d544733084
1 changed files with 12 additions and 0 deletions

View File

@ -2,6 +2,18 @@
# -*- mode: shell-script; indent-tabs-mode: nil; sh-basic-offset: 4; -*-
# ex: ts=8 sw=4 sts=4 et filetype=sh

is_ip() {
echo "$1" | {
IFS=. read a b c d
test "$a" -ge 0 -a "$a" -le 255 \
-a "$b" -ge 0 -a "$b" -le 255 \
-a "$c" -ge 0 -a "$c" -le 255 \
-a "$d" -ge 0 -a "$d" -le 255 \
2> /dev/null
} && return 0
return 1
}

get_ip() {
local iface="$1" ip=""
ip=$(ip -o -f inet addr show $iface)