summaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authortv <tv@shackspace.de>2015-05-20 22:05:38 +0200
committertv <tv@shackspace.de>2015-05-20 22:05:38 +0200
commit1cc154e1a25d4adaeb312370155417e910f20b1d (patch)
treebb83147686aa25a74b1b9e825d383c7adb244117 /lib
parent3cc354008a76f061ab4708922b7c0f3a8d37097b (diff)
lib/net.sh: add net_netmask_to_prefix
Diffstat (limited to 'lib')
-rw-r--r--lib/net.sh9
1 files changed, 9 insertions, 0 deletions
diff --git a/lib/net.sh b/lib/net.sh
new file mode 100644
index 00000000..518c955b
--- /dev/null
+++ b/lib/net.sh
@@ -0,0 +1,9 @@
+net_netmask_to_prefix() {(
+ binaryNetmask=$(echo $1 | sed 's/^/obase=2;/;s/\./;/g' | bc | tr -d \\n)
+ binaryPrefix=$(echo $binaryNetmask | sed -n 's/^\(1*\)0*$/\1/p')
+ if ! echo $binaryPrefix | grep -q .; then
+ echo $0: bad netmask: $netmask >&2
+ exit 4
+ fi
+ printf %s $binaryPrefix | tr -d 0 | wc -c
+)}