免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1542 | 回复: 0
打印 上一主题 下一主题

合并网段掩码位脚本一个 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-08-12 09:26 |只看该作者 |倒序浏览

                问题来源:
http://bbs2.chinaunix.net/thread-1197516-1-1.html
问题解决代码:
#!/bin/sh
# Variables and Function definition
PATH=$PATH:/sbin:/usr/sbin:/usr/local/bin
IPStart0=0;IPEnd0=0;
typeset TEMP_F
function IP2Long() {
        IP_ADDR="$1"
   echo ${IP_ADDR}|sed -e 's/[^0-9]/\ /g'|while read IP1 IP2 IP3 IP4 MASK;do
        IP_LONG="$((16777216*${IP1}+65536*${IP2}+256*${IP3}+${IP4}))"
        IP_SUM="$((${IP_LONG}+$((2**(32-${MASK:-32})))))"
        echo -e "${IP_LONG}\t${IP_SUM}"
   done
}
function Long2IP() {
   for i in IP1 IP2 IP3 IP4;do
        IP1=$(($1/16777216))
        IP2=$(($1%16777216/65536))
        IP3=$(($1%65536/256))
        IP4=$(($1%256))
   done
   if [ -z $2 ];then
        Mask="32"
   else
        Cnt=$(($2-$1))
        Mask=$(bc >${TEMP_F}
        IPStart0="${IPStart1}";IPEnd0="${IPEnd1}"
  elif [ ${IPEnd0} -ge ${IPEnd1} ];then
        :
  elif [ ${IPEnd0} -ge ${IPStart1} ];then
        typeset Cnt=$((${IPEnd1}-${IPStart0}))
    if [[ $(echo ${MaskDB[@]}|grep -E "\ ${Cnt}\ ") ]];then
        IPEnd0="${IPEnd1}"
    else
        Long2IP "${IPStart0}" "${IPEnd0}" >>${TEMP_F}
        IPStart0="${IPStart1}";IPEnd0="${IPEnd1}"
    fi
  fi
}
# Program main
[ -f ${1:-nofile} ] || exit 1
for i in $(seq 32);do MaskDB=("${MaskDB[@]}" $((2**${i})));done
TEMP_F=$(mktemp)
while read Line;do
  IP2Long ${Line}
done>${TEMP_F}; }
sed -e '/^0.0.0.0.*/d' -i ${TEMP_F}
if [[ $(wc -l $1|cut -f1 -d\ ) == $(wc -l ${TEMP_F}|cut -f1 -d\ ) ]];then
        cat ${TEMP_F}
else
        $0 ${TEMP_F}
fi
rm -f ${TEMP_F}
               
               
               
               
               

本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u/31455/showart_1120846.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP