#!/usr/bin/env bash

# - Convert CIDR to netmask
# -
cidr2mask() {
  local i mask=""
  local full_octets=$(($1/8))
  local partial_octet=$(($1%8))

  for ((i=0;i<4;i+=1)); do
    if [ $i -lt $full_octets ]; then
      mask+=255
    elif [ $i -eq $full_octets ]; then
      mask+=$((256 - 2**(8-$partial_octet)))
    else
      mask+=0
    fi  
    test $i -lt 3 && mask+=.
  done

  echo $mask
}

cidr=$1
mask=$(cidr2mask $cidr)

echo ""
echo "cidr: $cidr - mask: $mask"
echo ""
exit 0


