summaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authortv <tv@krebsco.de>2015-05-20 22:05:38 +0200
committertv <tv@krebsco.de>2015-05-20 22:05:38 +0200
commitc05afbe81aec1b3629c9527b4d9cd8f54f97943a (patch)
treebb83147686aa25a74b1b9e825d383c7adb244117 /lib
parente554c6a96718235a50d979daf2be1a8f41f3a2b5 (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 0000000..518c955
--- /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
+)}