Hi all, I would appreciate if someone could share how to convert CIDR notation to netmask and vice versa.
The value below is just an example. it could be different numbers/ip addresses.
CIDR to netmask is straightforward if tedious -- just binary math. Shells aren't generally that good at it but modern BASH has large enough numbers to handle this.
#!/bin/bash
while IFS="/" read IP S
do
M=$(( 0xffffffff ^ ((1 << (32-S)) -1) ))
echo "$IP netmask $(( (M>>24) & 0xff )).$(( (M>>16) & 0xff )).$(( (M>>8) & 0xff )).$(( M & 0xff ))"
done
If you have an older bash, it may generate negative values due to overflowing its 32-bit integers.
Awesome! Thanks Corona688 for the script. It's not what I'm looking for, but the script is good.
Here is my actual question, I've rephrased it and created separate topic for this.