Skip to content

Instantly share code, notes, and snippets.

@kwilczynski
Last active June 17, 2024 12:32
Show Gist options
  • Save kwilczynski/5d37e1cced7e76c7c9ccfdf875ba6c5b to your computer and use it in GitHub Desktop.
Save kwilczynski/5d37e1cced7e76c7c9ccfdf875ba6c5b 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 - $1)) - 1) ))
echo "$(( (value >> 24) & 0xff )).$(( (value >> 16) & 0xff )).$(( (value >> 8) & 0xff )).$(( value & 0xff ))"
}
@kwilczynski
Copy link
Author

@ckuhtz, I am glad you found it useful!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment