"My difficulty is I don't really understand well enough the code.."
Perhaps this demo program will help you
use strict;
use Socket;
my $len = '/29';
print "IP IPN Mask Subnet\n";
for my $i (1...31){
print_subnet('192.168.1.'.$i ,$len);
}
sub print_subnet {
my ( $ip, $len ) = @_;
my $ipn = unpack "N", Socket::inet_aton $ip;
$len =~ s/^\///;
my $maskn = 0xFFFFFFFF << ( 32 - $len );
my $subnet_ipn = $ipn & $maskn;
my $subnet_ip = Socket::inet_ntoa pack "N", $subnet_ipn;
printf "%-20s %s %x %s\n","$ip/$len",$ipn,$maskn,$subnet_ip;
}
It should produce a table like this. Change len values to see effect.
IP IPN Mask Subnet
192.168.1.1/30 3232235777 3fffffffc 192.168.1.0
192.168.1.2/30 3232235778 3fffffffc 192.168.1.0
192.168.1.3/30 3232235779 3fffffffc 192.168.1.0
192.168.1.4/30 3232235780 3fffffffc 192.168.1.4
192.168.1.5/30 3232235781 3fffffffc 192.168.1.4
192.168.1.6/30 3232235782 3fffffffc 192.168.1.4
192.168.1.7/30 3232235783 3fffffffc 192.168.1.4
192.168.1.8/30 3232235784 3fffffffc 192.168.1.8
192.168.1.9/30 3232235785 3fffffffc 192.168.1.8
etc
You should be able to add the first comment like this
$subnets{"$ip,$len"}{ipn} = $ipn;
$subnets{"$ip,$len"}{maskn} = $maskn;
if ( $subnets{"$ip,$len"}{'comment'} eq '' ){
$subnets{"$ip,$len"}{'comment'} = $comment;
}
Note : If you did this the comment would be erased by a later blank record
$subnets{"$ip,$len"} = { ipn => $ipn, maskn => $maskn };
To print it out use this
print "subnet,prefix,name\n";
print "$_,$subnet{$_}{'comment'}\n"
for sort { $subnets{$a}{ipn} <=> $subnets{$b}{ipn} };
poj
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.