Skip to content

Instantly share code, notes, and snippets.

@aachyee
Forked from linuxenko/cidr2mask
Created September 24, 2020 10:08
Show Gist options
  • Select an option

  • Save aachyee/125c5040f4649d54a68f505797268077 to your computer and use it in GitHub Desktop.

Select an option

Save aachyee/125c5040f4649d54a68f505797268077 to your computer and use it in GitHub Desktop.
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
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment