Example: I have this netmask: 255.255.255.0
Is there, in bash, a command or a simple script to convert my netmask in notation /24?
Function using subnetcalc
:
IPprefix_by_netmask() {
subnetcalc 1.1.1.1 "$1" -n | sed -n '/^Netw/{s#.*/ #/#p;q}'
}
In pure bash
, (i.e. no external utils like sed
or bc
), convert IP to a long octal string and sum its bits:
IPprefix_by_netmask () {
c=0 x=0$( printf '%o' ${1//./ } )
while [ $x -gt 0 ]; do
let c+=$((x%2)) 'x>>=1'
done
echo /$c ; }
Output of IPprefix_by_netmask 255.255.255.0
(either function):
/24
Based on Sasha's answer, this script works with dash
(tested with Ubuntu 18.04):
IPprefix_by_netmask() {
#function returns prefix for given netmask in arg1
bits=0
for octet in $(echo $1| sed 's/\./ /g'); do
binbits=$(echo "obase=2; ibase=10; ${octet}"| bc | sed 's/0//g')
bits=$(expr $bits + ${#binbits})
done
echo "/${bits}"
}
While GNU awk is not Bash, it’s installed by default in enough distributions that this may be helpful in the sense of the question:
awk -F. '{
split($0, octets)
for (i in octets) {
mask += 8 - log(2**8 - octets[i])/log(2);
}
print "/" mask
}' <<< 255.255.255.240
This prints:
/28
Example Function for RHEL6/RHEL7:
The Result:
In other Linux distributives ipcalc options may differ.
The same function without ipcalc, tested in Solaris and Linux: