Last active
June 17, 2024 12:32
-
-
Save kwilczynski/5d37e1cced7e76c7c9ccfdf875ba6c5b to your computer and use it in GitHub Desktop.
CIDR to netmask in bash.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 ))" | |
} |
@ckuhtz, I copied this from a larger script of mine, just to memoize it here for my future reference, and where there was something else there to do. But that was many years ago, and I don't quite remember. π Fixed now! Thank you!
Awesome! ππ»π very useful.
@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
Cool. But why $2 and not $1? :)