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 +++++++++ networking-configuration | 16 +++------------- 2 files changed, 12 insertions(+), 13 deletions(-) create mode 100644 lib/net.sh diff --git a/lib/net.sh b/lib/net.sh new file mode 100644 index 000000000..518c955b6 --- /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 +)} diff --git a/networking-configuration b/networking-configuration index 77b9a45be..0e105b45f 100755 --- a/networking-configuration +++ b/networking-configuration @@ -5,6 +5,7 @@ set -euf . ./lib/cac.sh +. ./lib/net.sh cac_servername=$1 hostname=$2 @@ -31,7 +32,7 @@ print_networking_configuraton() { gateway=$(echo $server | jq -r .gateway) nameserver=8.8.8.8 netmask=$(echo $server | jq -r .netmask) - prefixLength=$(netmaskToPrefixLengh $netmask) + prefix=$(net_netmask_to_prefix $netmask) # TODO generate all config and put it into a temp dir, then rsync that # @@ -43,7 +44,7 @@ print_networking_configuraton() { printf ' networking.interfaces.enp2s1.ip4 = [\n' printf ' {\n' printf ' address = "%s";\n' $address - printf ' prefixLength = %d;\n' $prefixLength + printf ' prefixLength = %d;\n' $prefix printf ' }\n' printf ' ];\n' printf ' networking.defaultGateway = "%s";\n' $gateway @@ -53,15 +54,4 @@ print_networking_configuraton() { printf '}\n' } -netmaskToPrefixLengh() { - 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 -} - - main "$@" -- cgit v1.2.3