【问题标题】:merging of multiple IPv4 address blocks on the basis of their country region根据国家/地区合并多个 IPv4 地址块
【发布时间】:2015-04-06 04:29:08
【问题描述】:

我有 CIDR 表示法形式的 IPv4 块。这些街区是根据城市划分的我想将它们合并一级我的意思是直到区域级别 例如

12.17.230.48|12.178.230.63|US|Texas|Temple
12.178.230.64|12.178.230.95|US|Texas|Abilene
12.178.230.96|12.178.230.111|US|Texas|Jayton
12.178.230.112|12.178.230.119|US|Texas|Wichita Falls
12.178.230.120|12.178.230.127|US|Texas|Mansfield
12.178.230.128|12.178.230.159|US|Texas|Waco (Bellmead)
12.178.230.160|12.178.230.167|US|Texas|Irving
12.178.230.168|12.178.230.175|US|Texas|Fort Worth
12.178.230.176|12.178.230.183|US|Texas|Alvarado
12.178.230.184|12.178.230.191|US|Texas|Weatherford
12.178.230.192|12.178.230.199|US|Texas|Haltom City
12.178.230.200|12.178.230.207|US|Texas|Fort Worth (Diamond Hill -    Jarvis)
12.178.230.208|12.178.230.223|US|Texas|Fort Worth
12.178.230.224|12.178.230.231|US|Texas|Coppell
12.178.230.232|12.178.230.239|US|Texas|Lubbock   

这里全部都去德克萨斯,所以我想像这样将所有这些块合并成一个

start_ip_of_first_range|end_ip_of_last_range_of_same_region|US|Texas

虽然我尝试过这样做,但看起来我什至违反了 IPv4 地址基本规则

请建议任何公式?

【问题讨论】:

    标签: networking ip ipv4 maxmind cidr


    【解决方案1】:

    您只需要确定“超网”的最低和最高 IP 子网。

    12.178.230.48|12.178.230.63|US|Texas|Temple
    12.178.230.232|12.178.230.239|US|Texas|Lubbock  
    

    因此,要将它们分组,您需要找到包含这些范围的子网掩码。您可以使用的唯一子网掩码是 /24/25 只会为您提供范围为 12.178.230.0-12.178.230.12712.178.230.128-12.178.230.255 的“超网”。

    /24 子网掩码为您提供以下范围:12.178.230.0-12.178.230.255

    额外: 超网可能不是正确的命名约定,但它让您大致了解这是您所有用户的全球 IP 范围 结果。

    【讨论】:

    • 加1分享超网知识
    【解决方案2】:

    好吧,写到这里已经很晚了,但是我找到了一个简单而冗长的方法来实现这个目标。

     1. First convert all the ranges in the form of network_address|subnet_mask
     2. sort whole the file in descending IP address
     3. Now check if two lines fulfill following conditions 
      a. Same subnet mast &&
      b. Re-Check network IP if the position of first set bit from right is greater than host bit length &&
      c. Network IP of the line = network ip of next line + 2^(number of host bit) +1 &&
      d. First set bit from right in first line should be equal and less than the first set bit in next line's IP. &&
      e. Both line should have same location &&
    4. In that case remove the next line and decrease the subnet mask length by one.
    5. Now the resultant range will serve exactly same IP addresses as much the initial two ranges were serving neither more or less .
    6. To make more merging keep repeating step 2 to 5 
    

    我使用 C 程序和 bash 实用程序完成了这项工作。虽然我没有在这里粘贴程序如果有人需要它,我会尝试在这里放置确切的解决方案。

    这可能是著名问题http://www.perlmonks.org/?node_id=118346的解决方案

    【讨论】:

      猜你喜欢
      • 2020-02-27
      • 2021-03-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-17
      • 1970-01-01
      • 2015-05-10
      • 1970-01-01
      相关资源
      最近更新 更多