Skip to content

Instantly share code, notes, and snippets.

@aachyee
Forked from kwilczynski/cidr-to-netmask.sh
Created September 24, 2020 16:30
Show Gist options
  • Select an option

  • Save aachyee/19856c63d04dcd2b1f67554639f4ecf8 to your computer and use it in GitHub Desktop.

Select an option

Save aachyee/19856c63d04dcd2b1f67554639f4ecf8 to your computer and use it in GitHub Desktop.
CIDR to netmask in bash.
# Return netmask for a given network and CIDR.
cidr_to_netmask() {
value=$(( 0xffffffff ^ ((1 << (32 - $2)) - 1) ))
echo "$(( (value >> 24) & 0xff )).$(( (value >> 16) & 0xff )).$(( (value >> 8) & 0xff )).$(( value & 0xff ))"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment