summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authortv <tv@shackspace.de>2015-08-06 19:37:27 +0200
committertv <tv@shackspace.de>2015-08-06 19:37:27 +0200
commitf4589158572ab35969b9bccf801ea07e115705e1 (patch)
tree8fc740eda2a54d9fad076e461ded91bb6944199f
parent0fc9cbeba4060380f698f51bb74081e2fcefadf3 (diff)
cac: import netmask_to_prefix from stockholm
-rwxr-xr-xcac16
1 files changed, 15 insertions, 1 deletions
diff --git a/cac b/cac
index 3f6e501..c6ab4e4 100755
--- a/cac
+++ b/cac
@@ -45,6 +45,20 @@ sed '
s/~/%7e/g
'
}
+netmask_to_prefix() {(
+#! /bin/sh
+set -euf
+
+netmask=$1
+
+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
+)}
#
cac_resources_cache=${cac_resources_cache-$HOME/tmp/cac_resources_cache.json}
@@ -196,7 +210,7 @@ __cac_cli__generatenetworking() {(
gateway=$(echo $server | jq -r .gateway)
nameserver=8.8.8.8
netmask=$(echo $server | jq -r .netmask)
- prefix=$(netmask-to-prefix $netmask)
+ prefix=$(netmask_to_prefix $netmask)
#printf '# Generated file: %s generatenetworking %s %s\n' "$0" "$1" "$2"
#printf '# on %s\n' "$(date -Is)"