专注各种脚本编程
Baidu
加入收藏夹
本站内容有下面分类知识,欢迎您的到来^_^
shell相关:指令篇 基础篇 脚本欣赏 编程实例 shell问问 shell视频教程 技巧篇 水平测试 E文资料 vi编辑器 高级Bash脚本编程指南
其他:mysql perl c语言 oracle
当前位置:| 主页>shell脚本欣赏>

计算networkid netmask broadcast

百度收藏 QQ搜藏

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 "\n$2 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${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

上一篇:mpc转mp3的脚本 下一篇:自动ftp脚本,可以用来更新named.root文件

power by soyo123 2007-2008