| 计算networkid netmask broadcast
For Expamle:
./net.sh 192.168.1.0 255.255.252.0
Network number: 192.168.0.0
Broadcast address: 192.168.3.255
Netmask bits: 22 计算networkid netmask broadcast
For Expamle:
./net.sh 192.168.1.0 255.255.252.0
Network number: 192.168.0.0
Broadcast address: 192.168.3.255
Netmask bits: 22
#!/bin/bash
if [ $# -ne 2 ];then
echo "Usage:./net.sh ipaddress netmask"
exit
fi
ip=(${1//[![:digit:]]/ })
echo "$ip"
mask=(${2//[![:digit:]]/ })
for i in $
do
j=7
tag=1
while [ $j -ge 0 ]
do
k=$((2**$j))
if [ $(( $i & $k )) -eq $k ]; then
if [ $tag -eq 1 ]; then
(( n += 1 ))
else
echo -e "\n is a bad netamsk with holes\n"
exit
fi
else
tag=0
fi
(( j -= 1 ))
done
done
for i in 0 1 2 3
do
a=$a$$(($ & $))
b=$b$$(($ | ($ ^ 255)))
done
echo Network number: $a
echo Broadcast address: $b
echo Netmask bits: $n |