计算网络广播地址的shell
时间:2005-09-04 来源:herotic
计算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 ${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 " $2 is a bad netamsk with holes " 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 Network number: $a echo Broadcast address: $b echo Netmask bits: $n