基于多个IPv4地址块的国家/地区对其进行合并



我有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地址的基本规则

请推荐一些配方奶粉?

您只需要最低和最高的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

额外:超网可能不是正确的命名约定,但它会让您大致了解这是所有后果

这里写得太晚了,但我找到了一种简单而冗长的方法来实现这个目标。

 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

相关内容

  • 没有找到相关文章

最新更新