免费注册 查看新帖 |

Chinaunix

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

做DNS VIEW,获取IP表后子网位如何批量合并?(已解决) [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-09-15 15:25 |只看该作者 |倒序浏览
目前我正在做智能DNS,,中间需要路由表,,但我用网上的方法获取到网通的IP表,里面居然有2千多条,一看都是没有合并过的,,手动一个一个的合需要太长的时间,请问如何能够利用程序或别的工具批量合并一下,,哪位知道能够帮帮我,感激不尽。。。

附件中有我通过whois.apnic.net获取到的所有网通IP子网。



以下为找到的解决方法:

  1. #!/bin/bash
  2. function IP2long(){
  3.   local IFS='.'
  4.   read IP1 IP2 IP3 IP4 <<<"$1"
  5.   echo $((IP1*256*256*256+IP2*256*256+IP3*256+IP4))
  6. }

  7. function long2IP(){
  8.   local IP=$1
  9.   local NET=
  10.   NET=".$(( IP - (IP >> 8 << 8 ) ))${NET}"
  11.   (( IP >>= 8 ))
  12.   NET=".$(( IP - (IP >> 8 << 8 ) ))${NET}"
  13.   (( IP >>= 8 ))
  14.   NET=".$(( IP - (IP >> 8 << 8 ) ))${NET}"
  15.   (( IP >>= 8 ))
  16.   NET="${IP}${NET}"
  17.   echo $NET
  18. }

  19. function cidr(){
  20.   local IFS='~'
  21.   local MASK=0
  22.   local HOSTS=0
  23.   read IPstart IPend <<<"$1"
  24.   start=$(IP2long $IPstart)
  25.   end=$(IP2long $IPend)
  26.   while [ $MASK -le 32 ]
  27.   do
  28.     if [ $(( start + ( 1 << MASK) -1 )) -eq $end ];then
  29.       echo "$IPstart/$(( 32-MASK))"
  30.       break
  31.     elif [ $(( start + (1 << MASK) -1 )) -gt $end ];then
  32.       ((MASK--))
  33.       echo "$IPstart/$(( 32-MASK))"
  34.       (( start += (1<<MASK) ))
  35.       IPstart=$(long2IP $start)
  36.       MASK=0
  37.     elif [ $(( (start>>MASK) % 2 )) -eq 1 ];then
  38.       echo "$IPstart/$(( 32-MASK))"
  39.       (( start += (1<<MASK) ))
  40.       IPstart=$(long2IP $start)
  41.       MASK=0
  42.     else
  43.       (( MASK ++ ))
  44.     fi
  45.   done
  46. }

  47. function formatIP(){
  48.   case $1 in
  49.     *~*)
  50.       echo $1
  51.       ;;
  52.     */*)
  53.       start=${1%/*}
  54.       mask=${1#*/}
  55.       if [ ${#mask} -gt 2 ];then
  56.         end=$(( $(IP2long $start) + (1<<32) - $(IP2long $mask) -1 ))
  57.       else
  58.         end=$(( $(IP2long $start) + (1 << (32-mask)) -1 ))
  59.       fi
  60.       echo "$start~$(long2IP $end)"
  61.       ;;
  62.     *)
  63.       echo
  64.       ;;
  65.   esac
  66. }

  67. read net1
  68. net1=$(formatIP $net1)
  69. start1=$(IP2long ${net1%~*})
  70. end1=$(IP2long ${net1#*~})
  71. while read net2
  72. do
  73.   net2=$(formatIP $net2)
  74.   start2=$(IP2long ${net2%~*})
  75.   end2=$(IP2long ${net2#*~})
  76.   if [ $end1 -ge $end2 ];then
  77.           continue
  78.   elif [ $((end1+1)) -eq $start2 ];then
  79.     end1=$end2
  80.   else
  81.     cidr "$(long2IP $start1)~$(long2IP $end1)"
  82.     start1=$start2
  83.     end1=$end2
  84.   fi
  85. done
  86. cidr "$(long2IP $start1)~$(long2IP $end1)"
复制代码


用法:
./CIDR.sh < 数据文件

需求:数据文件是经过排序的。

[ 本帖最后由 ttxx 于 2009-9-16 13:16 编辑 ]

cnc_acl.rar

3.17 KB, 下载次数: 53

论坛徽章:
5
IT运维版块每日发帖之星
日期:2015-08-06 06:20:00IT运维版块每日发帖之星
日期:2015-08-10 06:20:00IT运维版块每日发帖之星
日期:2015-08-23 06:20:00IT运维版块每日发帖之星
日期:2015-08-24 06:20:00IT运维版块每日发帖之星
日期:2015-11-12 06:20:00
2 [报告]
发表于 2009-09-15 15:59 |只看该作者
记得这里有人发过合并过的,你搜一下

论坛徽章:
0
3 [报告]
发表于 2009-09-15 17:41 |只看该作者
我搜索了很多。。好像没见。。。

[ 本帖最后由 ttxx 于 2009-9-15 17:43 编辑 ]

论坛徽章:
5
IT运维版块每日发帖之星
日期:2015-08-06 06:20:00IT运维版块每日发帖之星
日期:2015-08-10 06:20:00IT运维版块每日发帖之星
日期:2015-08-23 06:20:00IT运维版块每日发帖之星
日期:2015-08-24 06:20:00IT运维版块每日发帖之星
日期:2015-11-12 06:20:00
4 [报告]
发表于 2009-09-15 18:07 |只看该作者
ip route   58.17.128.0 0.127.255 10.0.0.2 track 1
ip route   58.17.160.0 0.3.255.255 10.0.0.2 track 1
ip route   58.18.0.0 0.0.255.255 10.0.0.2 track 1
ip route   58.19.0.0 0.127.255 10.0.0.2 track 1
ip route   58.21.0.0 0.0.255.255 10.0.0.2 track 1
ip route   58.240.0.0 0.0.255.255 10.0.0.2 track 1
ip route   58.240.63.0 0.255.255 10.0.0.2 track 1
ip route   58.240.166.0 0.255.255 10.0.0.2 track 1
ip route   58.241.73.0 0.255.255 10.0.0.2 track 1
ip route   58.244.0.0 0.0.255.255 10.0.0.2 track 1
ip route   60.0.0.0 0.7.255 10.0.0.2 track 1
ip route   60.8.0.0 0.3.255.255 10.0.0.2 track 1
ip route   60.10.128.0 0.3.255.255 10.0.0.2 track 1
ip route   60.12.0.0 0.0.255.255 10.0.0.2 track 1
ip route   60.12.7.0 0.255.255 10.0.0.2 track 1
ip route   60.13.0.0 0.63.255.255 10.0.0.2 track 1
ip route   60.13.128.0 0.127.255 10.0.0.2 track 1
ip route   60.14.0.0 0.0.255.255 10.0.0.2 track 1
ip route   60.16.0.0 0.7.255 10.0.0.2 track 1
ip route   60.24.0.0 0.3.255.255 10.0.0.2 track 1
ip route   60.28.0.0 0.0.255.255 10.0.0.2 track 1
ip route   60.28.3.0 0.255.255 10.0.0.2 track 1
ip route   60.28.4.0 0.3.255.255 10.0.0.2 track 1
ip route   60.28.8.0 0.255.255 10.0.0.2 track 1
ip route   60.28.64.0 0.0.7.255 10.0.0.2 track 1
ip route   60.28.72.0 0.0.1.255 10.0.0.2 track 1
ip route   60.28.80.0 0.0.15.255 10.0.0.2 track 1
ip route   60.28.96.0 0.0.1.255 10.0.0.2 track 1
ip route   60.30.0.0 0.0.255.255 10.0.0.2 track 1
ip route   60.30.0.0 0.0.1.255 10.0.0.2 track 1
ip route   60.30.2.0 0.255.255 10.0.0.2 track 1
ip route   60.30.4.0 0.0.1.255 10.0.0.2 track 1
ip route   60.31.0.0 0.0.255.255 10.0.0.2 track 1
ip route   60.208.0.0 0.15.255.255 10.0.0.2 track 1
ip route   60.217.0.0 0.0.15.255 10.0.0.2 track 1
ip route   60.255.0.0 0.0.255.255 10.0.0.2 track 1
ip route   61.52.0.0 0.3.255.255 10.0.0.2 track 1
ip route   61.133.0.0 0.127.255 10.0.0.2 track 1
ip route   61.134.96.0 0.0.31.255 10.0.0.2 track 1
ip route   61.134.128.0 0.127.255 10.0.0.2 track 1
ip route   61.136.0.0 0.127.255 10.0.0.2 track 1
ip route   61.137.128.0 0.127.255 10.0.0.2 track 1
ip route   61.138.0.0 0.63.255.255 10.0.0.2 track 1
ip route   61.138.64.0 0.63.255.255 10.0.0.2 track 1
ip route   61.138.128.0 0.63.255.255 10.0.0.2 track 1
ip route   61.139.128.0 0.63.255.255 10.0.0.2 track 1
ip route   61.156.0.0 0.0.255.255 10.0.0.2 track 1
ip route   61.158.0.0 0.0.255.255 10.0.0.2 track 1
ip route   61.159.0.0 0.63.255.255 10.0.0.2 track 1
ip route   61.161.0.0 0.63.255.255 10.0.0.2 track 1
ip route   61.161.128.0 0.127.255 10.0.0.2 track 1
ip route   61.162.0.0 0.0.255.255 10.0.0.2 track 1
ip route   61.167.0.0 0.0.255.255 10.0.0.2 track 1
ip route   61.168.0.0 0.0.255.255 10.0.0.2 track 1
ip route   61.176.0.0 0.0.255.255 10.0.0.2 track 1
ip route   61.179.0.0 0.0.255.255 10.0.0.2 track 1
ip route   61.179.119.0 0.255.255 10.0.0.2 track 1
ip route   61.180.128.0 0.127.255 10.0.0.2 track 1
ip route   61.181.0.0 0.0.255.255 10.0.0.2 track 1
ip route   61.181.26.0 0.255.255 10.0.0.2 track 1
ip route   61.181.244.0 0.255.255 10.0.0.2 track 1
ip route   61.181.248.0 0.255.255 10.0.0.2 track 1
ip route   61.182.0.0 0.0.255.255 10.0.0.2 track 1
ip route   61.189.0.0 0.127.255 10.0.0.2 track 1
ip route   124.88.0.0 0.0.255.255 10.0.0.2 track 1
ip route   124.89.0.0 0.127.255 10.0.0.2 track 1
ip route   125.32.0.0 0.0.255.255 10.0.0.2 track 1
ip route   125.40.0.0 0.7.255 10.0.0.2 track 1
ip route   202.38.164.0 0.3.255.255 10.0.0.2 track 1
ip route   202.75.208.0 0.0.7.255 10.0.0.2 track 1
ip route   202.96.64.0 0.0.31.255 10.0.0.2 track 1
ip route   202.97.128.0 0.0.31.255 10.0.0.2 track 1
ip route   202.97.160.0 0.0.31.255 10.0.0.2 track 1
ip route   202.97.192.0 0.63.255.255 10.0.0.2 track 1
ip route   202.97.224.0 0.255.255 10.0.0.2 track 1
ip route   202.97.226.0 0.255.255 10.0.0.2 track 1
ip route   202.97.234.0 0.255.255 10.0.0.2 track 1
ip route   202.98.0.0 0.0.31.255 10.0.0.2 track 1
ip route   202.99.64.0 0.63.255.255 10.0.0.2 track 1
ip route   202.99.128.0 0.63.255.255 10.0.0.2 track 1
ip route   202.99.192.0 0.0.31.255  10.0.0.2 track 1
ip route   202.99.224.0 0.0.31.255 10.0.0.2 track 1
ip route   202.102.128.0 0.63.255.255 10.0.0.2 track 1
ip route   202.102.136.0 0.255.255 10.0.0.2 track 1
ip route   202.102.224.0 0.0.31.255 10.0.0.2 track 1
ip route   202.106.133.0 0.255.255 10.0.0.2 track 1
ip route   202.106.143.0 0.255.255 10.0.0.2 track 1
ip route   202.107.0.0 0.127.255 10.0.0.2 track 1
ip route   202.108.146.0 0.0.1.255  10.0.0.2 track 1
ip route   202.108.181.0 0.255.255 10.0.0.2 track 1
ip route   202.110.0.0 0.63.255.255 10.0.0.2 track 1
ip route   202.110.64.0 0.63.255.255 10.0.0.2 track 1
ip route   202.110.192.0 0.63.255.255 10.0.0.2 track 1
ip route   202.110.210.0 0.255.255 10.0.0.2 track 1
ip route   202.110.224.0 0.255.255 10.0.0.2 track 1
ip route   202.111.128.0 0.63.255.255 10.0.0.2 track 1
ip route   202.122.128.0 0.255.255 10.0.0.2 track 1
ip route   202.127.209.0 0.255.255 10.0.0.2 track 1
ip route   210.75.144.0 0.0.15.255 10.0.0.2 track 1
ip route   210.76.32.0 0.0.31.255 10.0.0.2 track 1
ip route   210.76.160.0 0.0.31.255 10.0.0.2 track 1
ip route   211.155.232.0 0.3.255.255 10.0.0.2 track 1
ip route   211.162.16.0 0.0.15.255 10.0.0.2 track 1
ip route   218.7.0.0 0.0.255.255 10.0.0.2 track 1
ip route   218.8.0.0 0.3.255.255 10.0.0.2 track 1
ip route   218.10.53.0 0.255.255 10.0.0.2 track 1
ip route   218.12.0.0 0.0.255.255 10.0.0.2 track 1
ip route   218.21.128.0 0.127.255 10.0.0.2 track 1
ip route   218.24.0.0 0.0.255.255 10.0.0.2 track 1
ip route   218.26.0.0 0.0.255.255 10.0.0.2 track 1
ip route   218.28.0.0 0.0.255.255 10.0.0.2 track 1
ip route   218.28.68.0 0.255.255 10.0.0.2 track 1
ip route   218.56.0.0 0.3.255.255 10.0.0.2 track 1
ip route   218.56.32.0 0.255.255 10.0.0.2 track 1
ip route   218.56.48.0 0.255.255 10.0.0.2 track 1
ip route   218.56.111.0 0.255.255 10.0.0.2 track 1
ip route   218.57.131.0 0.255.255 10.0.0.2 track 1
ip route   218.57.136.0 0.255.255 10.0.0.2 track 1
ip route   218.57.139.0 0.255.255 10.0.0.2 track 1
ip route   218.57.185.0 0.255.255 10.0.0.2 track 1
ip route   218.57.212.0 0.255.255 10.0.0.2 track 1
ip route   218.58.54.0 0.255.255 10.0.0.2 track 1
ip route   218.58.59.0 0.255.255 10.0.0.2 track 1
ip route   218.58.76.0 0.255.255 10.0.0.2 track 1
ip route   218.60.0.0 0.0.255.255 10.0.0.2 track 1
ip route   218.62.0.0 0.127.255 10.0.0.2 track 1
ip route   218.62.33.0 0.255.255 10.0.0.2 track 1
ip route   218.67.128.0 0.127.255 10.0.0.2 track 1
ip route   218.67.240.0 0.3.255.255 10.0.0.2 track 1
ip route   218.68.0.0 0.0.255.255 10.0.0.2 track 1
ip route   218.68.212.0 0.255.255 10.0.0.2 track 1
ip route   218.69.89.0 0.255.255 10.0.0.2 track 1
ip route   218.69.93.0 0.255.255 10.0.0.2 track 1
ip route   218.69.95.0 0.255.255 10.0.0.2 track 1
ip route   218.69.96.0 0.255.255 10.0.0.2 track 1
ip route   218.69.102.0 0.255.255 10.0.0.2 track 1
ip route   218.69.250.0 0.255.255 10.0.0.2 track 1
ip route   218.104.96.0 0.0.15.255 10.0.0.2 track 1
ip route   218.106.96.0 0.3.255.255 10.0.0.2 track 1
ip route   218.246.224.0 0.0.15.255 10.0.0.2 track 1
ip route   219.154.0.0 0.0.255.255 10.0.0.2 track 1
ip route   219.156.0.0 0.0.255.255 10.0.0.2 track 1
ip route   219.158.0.0 0.0.31.255 10.0.0.2 track 1
ip route   219.158.26.0 0.255.255 10.0.0.2 track 1
ip route   219.158.32.0 0.0.31.255 10.0.0.2 track 1
ip route   219.159.0.0 0.63.255.255 10.0.0.2 track 1
ip route   219.232.64.0 0.0.31.255 10.0.0.2 track 1
ip route   220.114.96.0 0.0.31.255 10.0.0.2 track 1
ip route   220.248.128.0 0.0.31.255 10.0.0.2 track 1
ip route   221.0.0.0 0.3.255.255 10.0.0.2 track 1
ip route   221.5.128.0 0.127.255 10.0.0.2 track 1
ip route   221.5.152.0 0.255.255 10.0.0.2 track 1
ip route   221.6.0.0 0.0.255.255 10.0.0.2 track 1
ip route   221.8.0.0 0.0.255.255 10.0.0.2 track 1
ip route   221.8.12.0 0.255.255 10.0.0.2 track 1
ip route   221.10.0.0 0.0.255.255 10.0.0.2 track 1
ip route   221.11.0.0 0.127.255 10.0.0.2 track 1
ip route   221.11.128.0 0.63.255.255 10.0.0.2 track 1
ip route   221.11.192.0 0.0.31.255 10.0.0.2 track 1
ip route   221.11.224.0 0.0.31.255 10.0.0.2 track 1
ip route   221.12.0.0 0.127.255 10.0.0.2 track 1
ip route   221.12.128.0 0.63.255.255 10.0.0.2 track 1
ip route   221.13.0.0 0.0.255.255 10.0.0.2 track 1

好像可以的。

论坛徽章:
0
5 [报告]
发表于 2009-09-16 09:44 |只看该作者
不好意思。我可能没有说清楚。

表里有大量的:

58.20.0.0/16;
58.21.0.0/16;
58.22.0.0/16;
58.23.0.0/16;


其实合并一下只用一条就行了58.20.0.0/14

还有更多类似与这样的,,,但里面2千多条,,如果能够批量让他们自动合并的方法有吗?

论坛徽章:
5
IT运维版块每日发帖之星
日期:2015-08-06 06:20:00IT运维版块每日发帖之星
日期:2015-08-10 06:20:00IT运维版块每日发帖之星
日期:2015-08-23 06:20:00IT运维版块每日发帖之星
日期:2015-08-24 06:20:00IT运维版块每日发帖之星
日期:2015-11-12 06:20:00
6 [报告]
发表于 2009-09-16 09:50 |只看该作者
哦。
我没见过的。以前也又人问过,最后没结果。你看看我给你的能用不。能用就用吧。

论坛徽章:
0
7 [报告]
发表于 2009-09-16 12:54 |只看该作者
不行。DNS中用的ACL必须是子网位的。

不过还是谢谢你了。

论坛上以前问的也没有人回,看来有点难度,我去别的版块看看。

论坛徽章:
5
IT运维版块每日发帖之星
日期:2015-08-06 06:20:00IT运维版块每日发帖之星
日期:2015-08-10 06:20:00IT运维版块每日发帖之星
日期:2015-08-23 06:20:00IT运维版块每日发帖之星
日期:2015-08-24 06:20:00IT运维版块每日发帖之星
日期:2015-11-12 06:20:00
8 [报告]
发表于 2009-09-16 13:01 |只看该作者
哦。你自己算一下不就OK了。这个不难的。

论坛徽章:
0
9 [报告]
发表于 2009-09-16 13:16 |只看该作者
自己算是可以,但2千多条,,多费事啊。。不过还好有牛人懂网络,然后又写了shell

论坛徽章:
5
IT运维版块每日发帖之星
日期:2015-08-06 06:20:00IT运维版块每日发帖之星
日期:2015-08-10 06:20:00IT运维版块每日发帖之星
日期:2015-08-23 06:20:00IT运维版块每日发帖之星
日期:2015-08-24 06:20:00IT运维版块每日发帖之星
日期:2015-11-12 06:20:00
10 [报告]
发表于 2009-09-16 15:17 |只看该作者
嘿嘿。我一般用EXCEL来做,还是可以地。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP