Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re: Challenge Problem: Merging Network Addresses

by gbarr (Monk)
on Oct 13, 2001 at 03:17 UTC ( #118595=note: print w/ replies, xml ) Need Help??


in reply to Challenge Problem: Merging Network Addresses

After some discussion with merlyn on IRC, I came up with the following.

use Socket; my $str = ""; while(<>) { chomp; my ($ip,$bits) = m,^(\d+(?:\.\d+){3})/(\d+)$, or next; $str .= ";" . unpack("B*",inet_aton($ip)); substr($str,-$bits) = "#" x $bits if $bits = 32 - $bits; } $str .= ";"; 1 while $str =~ s/;([01]+)0(#*);\1.\2;/;${1}#${2};/g; my @blocks = $str =~ /[10#]+/g; foreach my $block (@blocks) { my $bits = 32 - ($block =~ tr/#/0/); print inet_ntoa(pack("B*", $block)),"/$bits\n"; }

Which actually, I think, shows some errors in 3 .out files on your site

--- nm3b.out Thu Oct 11 21:54:06 2001 +++ nm3b.xx Sat Oct 13 00:08:51 2001 @@ -1,4 +1,3 @@ -130.91.7.0/31 -130.91.7.2/31 +130.91.7.0/30 255.255.255.254/31 255.255.255.255/32 --- nm7.out Thu Oct 11 21:54:07 2001 +++ nm7.xx Sat Oct 13 00:09:10 2001 @@ -1,4 +1,3 @@ -1.2.3.0/28 -1.2.3.16/28 +1.2.3.0/27 6.6.6.0/27 6.6.7.0/26 --- nm8.out Thu Oct 11 21:54:07 2001 +++ nm8.xx Sat Oct 13 00:09:10 2001 @@ -1,5 +1,4 @@ -1.2.3.0/28 -1.2.3.16/28 -5.4.3.1/31 +1.2.3.0/27 +5.4.3.0/31 6.6.6.0/27 6.6.7.0/26

Update:

That code does not handle the following case

101.33.45.27/30 101.33.34.0/24

use Socket; my @blocks; while(<DATA>) { chomp; my ($ip,$bits) = m,^(\d+(?:\.\d+){3})/(\d+)$, or next; push @blocks, unpack("B*",inet_aton($ip)); substr($blocks[-1],-$bits) = "#" x $bits if $bits = 32 - $bits; } my $str = ";" . join(";", sort @blocks) . ";"; 1 while $str =~ s/;([01]+)(#*);\1[01#]*;/;${1}${2};/g; 1 while $str =~ s/;([01]+)0(#*);\1.\2;/;${1}#${2};/g; my @blocks = $str =~ /[10#]+/g; foreach my $block (@blocks) { my $bits = 32 - ($block =~ tr/#/0/); print inet_ntoa(pack("B*", $block)),"/$bits\n"; }


Comment on Re: Challenge Problem: Merging Network Addresses
Select or Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://118595]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (9)
As of 2014-10-25 15:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (145 votes), past polls