免费注册 查看新帖 |

Chinaunix

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

前两天看到ansanchen兄发的关于求助shell的帖子,看了以后深有同感。 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2006-04-15 18:36 |只看该作者 |倒序浏览
我现在有一组ip如下:
58.32.0.0/13
58.40.0.0/15
58.42.0.0/16
58.43.0.0/16
58.44.0.0/14
58.48.0.0/13
58.56.0.0/15
58.58.0.0/16
58.59.0.0/17
58.59.128.0/17
58.60.0.0/14
58.208.0.0/12
59.32.0.0/13
59.40.0.0/15
59.42.0.0/16
     .
大家可以看到上面这几个IP
58.32.0.0/13
58.40.0.0/15
58.42.0.0/16
58.43.0.0/16
58.44.0.0/14

其实可以写成
58.32.0.0/12
我就是但是这一堆IP中可能有不连续的也就是中间差几个B类或者查几个C类没有。我想知道怎样用shell能够把连续的IP给合并。只要是上下连续就合并,以此类推。


其实我也有同样的问题,看到几位高手用shell和perl编的程序,甚为佩服,我测试了一下,但和需求略为有些差异,结果有出入。前几天我找到freebsd ports/net-mgmt/aggregate,就是干这个的,但我比较笨,研究了半个月也没有弄明白怎么用,下面是man aggregate的内容,请各位老大给于指点。多谢

man aggregate
AGGREGATE(1)                                                      AGGREGATE(1)



NAME
       aggregate  -  optimise a list of route prefixes to help make nice short
       filters


SYNOPSIS
       aggregate [-m max-length] [-o max-opt-length] [-p default-length]  [-q]
       [-t] [-v]


DESCRIPTION
       Takes  a list of prefixes in conventional format on stdin, and performs
       two optimisations to attempt to reduce the length of the prefix list.

       The first optimisation is to remove any  supplied  prefixes  which  are
       superfluous  because they are already included in another supplied pre-
       fix. For example, 203.97.2.0/24 would be removed if  203.97.0.0/17  was
       also supplied.

       The  second  optimisation identifies adjacent prefixes that can be com-
       bined under a single, shorter-length prefix. For example, 203.97.2.0/24
       and 203.97.3.0/24 can be combined into the single prefix 203.97.2.0/23.


OPTIONS
       -m max-length
              Sets the maximum prefix  length  for  entries  read  from  stdin
              max_length bits. The default is 32. Prefixes with longer lengths
              will be discarded prior to processing.

       -o max-opt-length
              Sets the maximum prefix  length  for  optimisation  to  max-opt-
              length  bits.  The  default  is 32. Prefixes with longer lengths
              will not be subject to optimisation.

       -p default-length
              Sets the default prefix length. There  is  no  default;  without
              this  option  a  prefix  without  a  mask  length  is treated as
              invalid.  Use -p 32 -m 32 -o 32 to  aggregate  a  list  of  host
              routes specified as bare addresses, for example.

       -q     Sets quiet mode -- instructs aggregate never to generate warning
              messages or other output on stderr.

       -t     Silently truncate prefixes that seem  to  have  an  inconsistent
              prefix:  e.g. an input prefix 203.97.2.226/24 would be truncated
              to  203.97.2.0/24.  Without  this   option   an   input   prefix
              203.97.2.226/24  would  not be accepted, and a warning about the
              inconsistent mask would be generated.

       -v     Sets verbose mode. This changes the output format to display the
              source  line  number that the prefix was obtained from, together
              with a preceding "-" to indicate a route that can be suppressed,
              or  a  "+" to indicate a shorter-prefix aggregate that was added
              by aggregate as an adjacency  optimisation.  Note  that  verbose
              output continues even if -q is selected.

DIAGNOSTICS
       Aggregate exits 0 on success, and >0 if an error occurs.

EXAMPLES
       The following list of prefixes:

         193.58.204.0/22
         193.58.208.0/22
         193.193.160.0/22
         193.193.168.0/22
         193.243.164.0/22
         194.126.128.0/22
         194.126.132.0/22
         194.126.134.0/23
         194.151.128.0/19
         195.42.240.0/21
         195.240.0.0/16
         195.241.0.0/16

       is optimised as followed by aggregate (output shown using the -v flag):

         aggregate: maximum prefix length permitted will be 24
         [    0] + 193.58.204.0/21
         [    1] - 193.58.204.0/22
         [    2] - 193.58.208.0/22
         [    3]   193.193.160.0/22
         [    4]   193.193.168.0/22
         [    5]   193.243.164.0/22
         [    0] + 194.126.128.0/21
         [    6] - 194.126.128.0/22
         [    7] - 194.126.132.0/22
         [    8] - 194.126.134.0/23
         [    9]   194.151.128.0/19
         [   10]   195.42.240.0/21
         [    0] + 195.240.0.0/15
         [   11] - 195.240.0.0/16
         [   12] - 195.241.0.0/16

       Note that 193.58.204.0/22 and 193.58.208.0/22 were combined  under  the
       single  prefix  193.58.204.0/21,  and  194.126.134.0/23  was suppressed
       because it was included  in  194.126.132.0/22.  The  number  in  square
       brackets at the beginning of each line indicates the original line num-
       ber, or zero for new prefixes that were introduced by aggregate.

       The output without the -v flag is as follows:

         193.58.204.0/21
         193.193.160.0/22
         193.193.168.0/22
         193.243.164.0/22
         194.126.128.0/21
         194.151.128.0/19
         195.42.240.0/21
         195.240.0.0/15

SEE ALSO
       aggregate-ios(1)

HISTORY
       Aggregate was written by Joe Abley <jabley@mfnx.net>, and has been rea-
       sonably  well  tested. It is suitable for reducing customer prefix fil-
       ters for production use without extensive hand-proving of results.

       Autoconf     bits     were     donated     by      Michael      Shields
       <michael.shields@mfn.com>.   The -t option was suggested by Robin John-
       son <robbat2@fermi.orbis-terrarum.net>, and the  treatment  of  leading
       zeros  on octet parsing was changed following comments from Arnold Nip-
       per <arnold@nipper.de>.

       An early version of aggregate would attempt to  combine  adjacent  pre-
       fixes  regardless of whether the first prefix lay on an appropriate bit
       boundary or not (pointed out with  great  restraint  by  Robert  Noland
       <rnoland@2hip.net>).

BUGS
       Common unix parsing of IPv4 addresses understands the representation of
       individual octets in octal or hexadecimal, following a "0" or "0x" pre-
       fix, respectively. That convention has been deliberately disabled here,
       since resources such as the IRR do not follow the convention, and  con-
       fusion can result.

       For  extremely  sensitive  applications, judicious use of the -v option
       together with a pencil and paper is probably advisable.




Joe Abley                       2001 November 2                   AGGREGATE(1)
AGGREGATE(1)                                                      AGGREGATE(1)



NAME
       aggregate  -  optimise a list of route prefixes to help make nice short
       filters


SYNOPSIS
       aggregate [-m max-length] [-o max-opt-length] [-p default-length]  [-q]
       [-t] [-v]


DESCRIPTION
       Takes  a list of prefixes in conventional format on stdin, and performs
       two optimisations to attempt to reduce the length of the prefix list.

       The first optimisation is to remove any  supplied  prefixes  which  are
       superfluous  because they are already included in another supplied pre-
       fix. For example, 203.97.2.0/24 would be removed if  203.97.0.0/17  was
       also supplied.

       The  second  optimisation identifies adjacent prefixes that can be com-
       bined under a single, shorter-length prefix. For example, 203.97.2.0/24
       and 203.97.3.0/24 can be combined into the single prefix 203.97.2.0/23.


OPTIONS
       -m max-length
              Sets the maximum prefix  length  for  entries  read  from  stdin
              max_length bits. The default is 32. Prefixes with longer lengths
              will be discarded prior to processing.

       -o max-opt-length
              Sets the maximum prefix  length  for  optimisation  to  max-opt-
              length  bits.  The  default  is 32. Prefixes with longer lengths
              will not be subject to optimisation.

       -p default-length
              Sets the default prefix length. There  is  no  default;  without
              this  option  a  prefix  without  a  mask  length  is treated as
              invalid.  Use -p 32 -m 32 -o 32 to  aggregate  a  list  of  host
              routes specified as bare addresses, for example.

       -q     Sets quiet mode -- instructs aggregate never to generate warning
              messages or other output on stderr.

       -t     Silently truncate prefixes that seem  to  have  an  inconsistent
              prefix:  e.g. an input prefix 203.97.2.226/24 would be truncated
              to  203.97.2.0/24.  Without  this   option   an   input   prefix
              203.97.2.226/24  would  not be accepted, and a warning about the
              inconsistent mask would be generated.

       -v     Sets verbose mode. This changes the output format to display the
              source  line  number that the prefix was obtained from, together
              with a preceding "-" to indicate a route that can be suppressed,
              or  a  "+" to indicate a shorter-prefix aggregate that was added
              by aggregate as an adjacency  optimisation.  Note  that  verbose
              output continues even if -q is selected.

DIAGNOSTICS
       Aggregate exits 0 on success, and >0 if an error occurs.

EXAMPLES
       The following list of prefixes:

         193.58.204.0/22
         193.58.208.0/22
         193.193.160.0/22
         193.193.168.0/22
         193.243.164.0/22
         194.126.128.0/22
         194.126.132.0/22
         194.126.134.0/23
         194.151.128.0/19
         195.42.240.0/21
         195.240.0.0/16
         195.241.0.0/16

       is optimised as followed by aggregate (output shown using the -v flag):

         aggregate: maximum prefix length permitted will be 24
         [    0] + 193.58.204.0/21
         [    1] - 193.58.204.0/22
         [    2] - 193.58.208.0/22
         [    3]   193.193.160.0/22
         [    4]   193.193.168.0/22
         [    5]   193.243.164.0/22
         [    0] + 194.126.128.0/21
         [    6] - 194.126.128.0/22
         [    7] - 194.126.132.0/22
         [    8] - 194.126.134.0/23
         [    9]   194.151.128.0/19
         [   10]   195.42.240.0/21
         [    0] + 195.240.0.0/15
         [   11] - 195.240.0.0/16
         [   12] - 195.241.0.0/16

       Note that 193.58.204.0/22 and 193.58.208.0/22 were combined  under  the
       single  prefix  193.58.204.0/21,  and  194.126.134.0/23  was suppressed
       because it was included  in  194.126.132.0/22.  The  number  in  square
       brackets at the beginning of each line indicates the original line num-
       ber, or zero for new prefixes that were introduced by aggregate.

       The output without the -v flag is as follows:

         193.58.204.0/21
         193.193.160.0/22
         193.193.168.0/22
         193.243.164.0/22
         194.126.128.0/21
         194.151.128.0/19
         195.42.240.0/21
         195.240.0.0/15

SEE ALSO
       aggregate-ios(1)

HISTORY
       Aggregate was written by Joe Abley <jabley@mfnx.net>, and has been rea-
       sonably  well  tested. It is suitable for reducing customer prefix fil-
       ters for production use without extensive hand-proving of results.

       Autoconf     bits     were     donated     by      Michael      Shields
       <michael.shields@mfn.com>.   The -t option was suggested by Robin John-
       son <robbat2@fermi.orbis-terrarum.net>, and the  treatment  of  leading
       zeros  on octet parsing was changed following comments from Arnold Nip-
       per <arnold@nipper.de>.

       An early version of aggregate would attempt to  combine  adjacent  pre-
       fixes  regardless of whether the first prefix lay on an appropriate bit
       boundary or not (pointed out with  great  restraint  by  Robert  Noland
       <rnoland@2hip.net>).

BUGS
       Common unix parsing of IPv4 addresses understands the representation of
       individual octets in octal or hexadecimal, following a "0" or "0x" pre-
       fix, respectively. That convention has been deliberately disabled here,
       since resources such as the IRR do not follow the convention, and  con-
       fusion can result.

       For  extremely  sensitive  applications, judicious use of the -v option
       together with a pencil and paper is probably advisable.




Joe Abley                       2001 November 2                   AGGREGATE(1)
root[/home]#

论坛徽章:
1
荣誉版主
日期:2011-11-23 16:44:17
2 [报告]
发表于 2006-04-15 22:53 |只看该作者
恩?
我覺得 193.58.204.0/21 跟 193.58.208.0/21 是不同的 network 啊.
怎麼會并成一起呢?

204 的 binary 是: 11001100
以 22bit mask 來計算的話, network 部份是 11001100
以 21bit mask 來計算的話, network 部份是 11001000
208 的 binary 是11010000
以 22bit mask 來計算的話, network 部份是 11010000
以 21bit mask 來計算的話, network 部份是 11010000

很明顯, 如下兩個 network 是不一樣的:
11001000
11010000

除非用到 19bit 的 mask .

论坛徽章:
0
3 [报告]
发表于 2006-04-15 23:07 |只看该作者

佩服,楼上的大哥真是心细,求真务实。

佩服,我这段内容看了几遍,居然没有看出来作者这个错误,193.58.204.0/21这个地址段的网络地址应该是193.58.200.0/21,是不包含193.58.208.0的

论坛徽章:
1
荣誉版主
日期:2011-11-23 16:44:17
4 [报告]
发表于 2006-04-15 23:40 |只看该作者
不過, 也很感謝您提供的資料.
也讓我發現在ansanchen兄那貼裡寫的 shell script 之不足.
最後的貼, 對如下這幾個 network 計算, 會漏掉最後 23bit 的那個:
         194.126.128.0/22
         194.126.132.0/22
         194.126.134.0/23
事實上, 應該是并在同一 subnet 的.

我正嘗試修改這個 bug , 不過, 還沒完成....

p.s.
ansanchen兄的貼子可參考:
http://bbs.chinaunix.net/viewthr ... &extra=page%3D1

论坛徽章:
0
5 [报告]
发表于 2006-04-17 10:18 |只看该作者
帮你解释一下这个问题吧,呵呵
估计你的疑问在这里吧
194.126.128.0/22
194.126.132.0/22
194.126.134.0/23

为什么aggregate的结果是194.126.128.0/21
而我的是(就是那个perl的代码)
194.126.128.0/21
194.126.134.0/23
会比他多一个,其实这两个都是对的
因为194.126.128.0/21本身就包含了194.126.134.0/23这个子网
因为194.126.128.0/21的地址范围是194.126.128.0到194.126.135.255
194.126.134.0/23的地址范围是194.126.134.0到194.126.135.255
但为什么我们的结果不同呢,是因为前提条件不同,因为aggregate的方法是合并尽量多的子网为超网,
而我的代码是为了符合那位ansanchen朋友的要求前后ip地址连续才做子网合并超网的
194.126.128.0/22 的ip地址范围194.126.128.0-194.126.131.255
194.126.132.0/22的ip地址范围194.126.132.0-194.126.135.255
194.126.134.0/23的ip地址范围194.126.134.0-194.126.135.255
其中前两个是连续地址范围所以我就行前两个的合并,而且其实ansanchen朋友是从电信下载的ip地址范围列表,所以其实不会出现这种重复地址划分的问题,所以我不用考虑包含更多的子网问题,这就是为什么答案不同的原因,呵呵!!

[ 本帖最后由 slash001 于 2006-4-17 10:21 编辑 ]
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP