]> 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>
Wed, 12 Aug 2015 12:24:05 +0000 (14:24 +0200)
add function to test if string is a valid IP

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

index 68bc09524dc6e2e19cc0a9c9f5d1d2ee9fe606f0..5c65a2ea83200563c557b35069d86553a2a8fe59 100755 (executable)
@@ -1,5 +1,17 @@
 #!/bin/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)