ip="192.168.12.23"
mask="23"
echo "input:${ip}/${mask}"
msk_end()
{
declare -i FULL_MASK_INT=4294967295
declare -i RIGHT_MOVE=$1
declare -i N="${FULL_MASK_INT} >> ${RIGHT_MOVE}"
declare -i H1="$N & 0x000000ff"
declare -i H2="($N & 0x0000ff00) >> 8"
declare -i L1="($N & 0x00ff0000) >> 16"
declare -i L2="($N & 0xff000000) >> 24"
echo "$L2.$L1.$H2.$H1"
}
msk()
{
#0xFFFFFFFF
declare -i FULL_MASK_INT=4294967295
declare -i MASK_LEN=$1
declare -i LEFT_MOVE="32 - ${MASK_LEN}"
#if mask is 24, N is 0xFFFFFF00
declare -i N="${FULL_MASK_INT} << ${LEFT_MOVE}"
declare -i H1="$N & 0x000000ff"
declare -i H2="($N & 0x0000ff00) >> 8"
declare -i L1="($N & 0x00ff0000) >> 16"
declare -i L2="($N & 0xff000000) >> 24"
echo "$L2.$L1.$H2.$H1"
}
ipend()
{
local cc=""
for i in 1 2 3 4
do
let "aa = $(echo $1 | cut -d '.' -f $i) | $(echo $2 | cut -d '.' -f $i)"
if [ -n "$cc" ]
then
cc="$cc.$aa"
else
cc="$aa"
fi
done
echo "$cc"
}
ipstart()
{
local cc=""
for i in 1 2 3 4
do
let "aa = $(echo $1 | cut -d '.' -f $i) & $(echo $2 | cut -d '.' -f $i)"
if [ -n "$cc" ]
then
cc="$cc.$aa"
else
cc="$aa"
fi
done
echo "$cc"
}
maskstart=$(msk $mask)
maskend=$(msk_end $mask)
dd=$(ipstart $ip $maskstart)
ddend=$(ipend $ip $maskend)
echo "output:${dd}-${ddend}"