一款IP合并和分解工具

一:需求说明

         近期在工作中有个需求,需要将七千多个ip地址(有的带掩码,有的不带掩码)进行合并尝试,看能不能通过合并减少ip的条目数。这就涉及到ip和掩码的计算,举例如下:

192.168.1.0/25
192.168.1.128/29
192.168.1.136/30
192.168.1.140/32
192.168.1.141/32
192.168.1.142/31
192.168.1.144/28
192.168.1.160/27
192.168.1.192/29
192.168.1.200/30
192.168.1.204/31
192.168.1.206/32
192.168.1.207/32
192.168.1.208/28
192.168.1.224/27

如上的ip,我么可以先对其进行展开,直观的看到其ip地址范围

192.168.1.0-192.168.1.127
192.168.1.128-192.168.1.135
192.168.1.136-192.168.1.139
192.168.1.140
192.168.1.141
192.168.1.142-192.168.1.143
192.168.1.144-192.168.1.159
192.168.1.160-192.168.1.191
192.168.1.192-192.168.1.199
192.168.1.200-192.168.1.203
192.168.1.204-192.168.1.205
192.168.1.206
192.168.1.207
192.168.1.208-192.168.1.223
192.168.1.224-192.168.1.255

这里插一下怎么去展开地址,我们借助IPy模块去做展开

import os
import IPy
from ip_info import IPList

for ip in IPList:
    ipIns = IPy.IP(ip)
    print(ipIns.strFullsize(3))

        先把所有地址读到一个列表里,再遍历列表,用strFullsize方法进行展开,这里就不做详细介绍了,有兴趣的同学可以自行去code调试

        从上面的地址可以清除的看到所有地址合并就是192.168.1.0-192.168.1.255,可以直接用192.168.1.0/24一条去表示上面的所有条目。条目少我们可以手工计算的方式进行合并,但是如果有几千上万个ip条目手工计算就不太现实了。

        github上有一款ip合并和分解的工具cidr-merger,可以很好的解决咱们的需求

gituhub上的工程地址如下:GitHub - zhanhb/cidr-merger: A simple command line tool to merge ip/ip cidr/ip range, supports IPv4/IPv6A simple command line tool to merge ip/ip cidr/ip range, supports IPv4/IPv6 - GitHub - zhanhb/cidr-merger: A simple command line tool to merge ip/ip cidr/ip range, supports IPv4/IPv6https://github.com/zhanhb/cidr-merger

 其发行版本提供了linux版本,windowd版本,我们可以根据需要去下载对应的版本。这里我是在windows上使用。

二:工具使用

下面我们试用一下该款工具

1,查看工具使用帮助

2, IP 段分解为掩码形式

 3,IP段合并

 我们尝试去打乱顺序,并且加一条重复的ip,看工具能不能正确处理

192.168.1.136/30
192.168.1.140/32
192.168.1.0/25
192.168.1.128/29
192.168.1.140/32

 可以看到工具能很好的处理

那我们回到最上面的需求,我们将IP拷贝到ip.txt里去,用工具运行看下

 可以看到合并成了一条,我们可以通过-r参数以ip范围的方式输出,也可以通过-s以掩码的方式输出,如果我们输入的结果比较多,输出的结果也比较多,除了用重定向符号输出到文件,也可以通过-o参数,这样便于后续的程序处理

 4,官网示例

通过上面的参数可以看到该款工具还有很多其他的玩法,在此就不一一举例了,有些伙伴可能不能访问github,在这贴上官网的用例

$ echo '1.0.0.1-223.255.255.254' | cidr-merger
> 1.0.0.1
  1.0.0.2/31
  1.0.0.4/30
  1.0.0.8/29
  ......
  1.128.0.0/9
  2.0.0.0/7
  4.0.0.0/6
  8.0.0.0/5
  16.0.0.0/4
  32.0.0.0/3
  64.0.0.0/2
  128.0.0.0/2
  192.0.0.0/4
  208.0.0.0/5
  216.0.0.0/6
  220.0.0.0/7
  222.0.0.0/8
  223.0.0.0/9
  ......
  223.255.255.240/29
  223.255.255.248/30
  223.255.255.252/31
  223.255.255.254
$ echo '1.1.1.0' > a; \
    echo '1.1.1.1' > b; \
    echo '1.1.1.2/31' > c; \
    echo '1.1.1.3-1.1.1.7' > d; \
    cidr-merger -o merge a b c d; \
    cat merge; \
    rm a b c d merge
> 1.1.1.0/29
$ wget -O- "https://ftp.apnic.net/stats/apnic/`TZ=UTC date +%Y`/delegated-apnic-`TZ=UTC+24 date +%Y%m%d`.gz" | \
    gzip -d | awk -F\| '!/^\s*(#.*)?$/&&/CN\|ipv4/{print $4 "/" 32-log($5)/log(2)}' | \
    cidr-merger -eo/etc/chinadns_chnroute.txt # update ip on router
$ #              ^ e: means error if input is empty
$ echo 'fe80::/10' | cidr-merger -r
> fe80::-febf:ffff:ffff:ffff:ffff:ffff:ffff:ffff
$ echo '1.1.1.0' > a; echo '1.1.1.1' | cidr-merger - a; rm a
$ #                                                ^ -: means standard input
> 1.1.1.0/31

 -r和-s输出的比较

$ echo '1.1.1.1/32' | cidr-merger
> 1.1.1.1
$ echo '1.1.1.1/32' | cidr-merger -s
> 1.1.1.1/32
$ echo '1.1.1.1/32' | cidr-merger -r
> 1.1.1.1
$ echo '1.1.1.1/32' | cidr-merger -rs
> 1.1.1.1-1.1.1.1

空策略的比较

$ cidr-merger -o txt /dev/null # an empty file named `txt` is created.
$ cidr-merger -ko txt /dev/null # no file is created, and this program exit with code zero
$ #            ^ same as `cat /dev/null | cidr-merger --skip-empty --output txt`
$ cidr-merger -eo txt /dev/null # no file is created, and this program exit with code non zero
$ #            ^ same as `cat /dev/null | cidr-merger --error-if-empty --output txt`
$ # option `-e` might be useful when download file from internet and then write to a file

$ # There is no difference if you redirect output to a file such as following
$ cat /dev/null | cidr-merger -e > txt
  # file `txt` is created, but this program exit with code non zero

 为方便小伙伴下载已上传到csdn:https://mp.csdn.net/mp_download/manage/download/UpDetailed

 后续如果有时间想设计一个可视化工具,通过可视化来展现这一款工具的能力

猜你喜欢

转载自blog.csdn.net/qq_27071221/article/details/130781304
今日推荐