2015-02-06 51 views
2

我有CIDR表示法中的IPv4塊。這些塊城市的基礎上,分我想將它們合併一個級別了,我的意思是,直到區域級 以其國家/地區爲基礎合併多個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地址基本規則

請提出的公式?

回答

1

那麼它遲到寫在這裏,但我找到了一個簡單和冗長的方式來實現這一目標。

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

2

所有你需要的是確定'supernet'的最低和最高的IP子網。

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

因此,要將它們分組,您需要找到包含這些範圍的子網掩碼。您可以使用的唯一子網掩碼是/24。 A /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範圍爲您所有的 結果。

+0

加1分享關於超網知識 – Akaks 2015-02-06 08:24:00

相關問題