From d5447330844a1f16d927d42cd1d96c5425133aad Mon Sep 17 00:00:00 2001 From: Harald Hoyer Date: Wed, 12 Aug 2015 14:24:05 +0200 Subject: [PATCH] network/net-lib.sh: add is_ip() add function to test if string is a valid IP (cherry picked from commit 01b23b6900eabefbfd1f589b9f12c8ff38c5afc0) --- modules.d/40network/net-lib.sh | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/modules.d/40network/net-lib.sh b/modules.d/40network/net-lib.sh index cfc06fe..44c1bf0 100755 --- a/modules.d/40network/net-lib.sh +++ b/modules.d/40network/net-lib.sh @@ -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)