Harald Hoyer bb31e7
From 01b23b6900eabefbfd1f589b9f12c8ff38c5afc0 Mon Sep 17 00:00:00 2001
Harald Hoyer bb31e7
From: Harald Hoyer <harald@redhat.com>
Harald Hoyer bb31e7
Date: Wed, 12 Aug 2015 14:24:05 +0200
Harald Hoyer bb31e7
Subject: [PATCH] network/net-lib.sh: add is_ip()
Harald Hoyer bb31e7
Harald Hoyer bb31e7
add function to test if string is a valid IP
Harald Hoyer bb31e7
---
Harald Hoyer bb31e7
 modules.d/40network/net-lib.sh | 12 ++++++++++++
Harald Hoyer bb31e7
 1 file changed, 12 insertions(+)
Harald Hoyer bb31e7
Harald Hoyer bb31e7
diff --git a/modules.d/40network/net-lib.sh b/modules.d/40network/net-lib.sh
Harald Hoyer bb31e7
index 68bc095..5c65a2e 100755
Harald Hoyer bb31e7
--- a/modules.d/40network/net-lib.sh
Harald Hoyer bb31e7
+++ b/modules.d/40network/net-lib.sh
Harald Hoyer bb31e7
@@ -1,5 +1,17 @@
Harald Hoyer bb31e7
 #!/bin/sh
Harald Hoyer bb31e7
 
Harald Hoyer bb31e7
+is_ip() {
Harald Hoyer bb31e7
+    echo "$1" | {
Harald Hoyer bb31e7
+        IFS=. read a b c d
Harald Hoyer bb31e7
+        test "$a" -ge 0 -a "$a" -le 255 \
Harald Hoyer bb31e7
+             -a "$b" -ge 0 -a "$b" -le 255 \
Harald Hoyer bb31e7
+             -a "$c" -ge 0 -a "$c" -le 255 \
Harald Hoyer bb31e7
+             -a "$d" -ge 0 -a "$d" -le 255 \
Harald Hoyer bb31e7
+             2> /dev/null
Harald Hoyer bb31e7
+    } && return 0
Harald Hoyer bb31e7
+    return 1
Harald Hoyer bb31e7
+}
Harald Hoyer bb31e7
+
Harald Hoyer bb31e7
 get_ip() {
Harald Hoyer bb31e7
     local iface="$1" ip=""
Harald Hoyer bb31e7
     ip=$(ip -o -f inet addr show $iface)