Script to get network number and broadcast ad..
时间:2004-10-13 来源:lightspeed
$ ./1 192.168.0.1 255.255.255.0
Network number: 192.168.0.0
Broadcast address: 192.168.0.255
Netmask bits: 24
$
$ ./1 192.168.0.1 255.255.255.192
Network number: 192.168.0.0
Broadcast address: 192.168.0.63
Netmask bits: 26
$
$ ./1 192.168.0.1 255.255.255.191
255.255.255.191 is a bad netamsk with holes
$ ./1 10.0.0.1 252.0.0.0
Network number: 8.0.0.0
Broadcast address: 11.255.255.255
Netmask bits: 6
Ksh vresion:
#!/bin/ksh
set -A ip ${1//(D)/ }
set -A mask ${2//(D)/ }
for i in ${mask[*]}
do
j=7
tag=1
while [ $j -ge 0 ]
do
k=$((pow(2,$j)))
if [ $(( $i & $k )) -eq $k ]; then
if [ $tag -eq 1 ]; then
(( n++ ))
else
echo "n$2 is a bad netamsk with holesn"
exit
fi
else
tag=0
fi
(( j -- ))
done
done
for i in 0 1 2 3
do
a=$a${a:+.}$((${ip[i]} & ${mask[i]}))
b=$b${b:+.}$((${ip[i]} | (${mask[i]} ^ 255)))
done
echo
echo Network number: $a
echo Broadcast address: $b
echo Netmask bits: $n
# ---------- end -------------
Bash vresion:
#!/bin/bash
ip=(${1//[![:digit:]]/ })
mask=(${2//[![:digit:]]/ })
for i in ${mask[*]}
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$2 is a bad netamsk with holesn"
exit
fi
else
tag=0
fi
(( j -= 1 ))
done
done
for i in 0 1 2 3
do
a=$a${a:+.}$((${ip[i]} & ${mask[i]}))
b=$b${b:+.}$((${ip[i]} | (${mask[i]} ^ 255)))
done
echo
echo Network number: $a
echo Broadcast address: $b
echo Netmask bits: $n