]> git.ipfire.org Git - thirdparty/dracut.git/commitdiff
network/net-lib.sh: add is_ip()
authorHarald Hoyer <harald@redhat.com>
Wed, 12 Aug 2015 12:24:05 +0000 (14:24 +0200)
committerHarald Hoyer <harald@redhat.com>
Thu, 13 Aug 2015 13:25:24 +0000 (15:25 +0200)
add function to test if string is a valid IP

(cherry picked from commit 01b23b6900eabefbfd1f589b9f12c8ff38c5afc0)

modules.d/40network/net-lib.sh

index cfc06feb4009ecf2031b2c0ee8a57ffe5e6628b2..44c1bf0495f7db576c1626b07a034b33b6e83518 100755 (executable)
@@ -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)