From 1cc154e1a25d4adaeb312370155417e910f20b1d Mon Sep 17 00:00:00 2001 From: tv Date: Wed, 20 May 2015 22:05:38 +0200 Subject: lib/net.sh: add net_netmask_to_prefix --- lib/net.sh | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 lib/net.sh (limited to 'lib') 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 +)} -- cgit v1.2.3