Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options

Re: How to include a variable in output for derived subnets

by poj (Prior)
on Jun 22, 2013 at 17:16 UTC ( #1040280=note: print w/replies, xml ) Need Help??

in reply to How to include a variable in output for derived subnets

"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 3232235777 3fffffffc 3232235778 3fffffffc 3232235779 3fffffffc 3232235780 3fffffffc 3232235781 3fffffffc 3232235782 3fffffffc 3232235783 3fffffffc 3232235784 3fffffffc 3232235785 3fffffffc 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} };

Replies are listed 'Best First'.
Re^2: How to include a variable in output for derived subnets
by stroke (Acolyte) on Jun 24, 2013 at 11:09 UTC

    Hi poj,

    Thanks - that pretty much did the trick. I had to tweak the print code block a little as I got a compilation error, but it works a treat. Appreciate your assistance.

    As is the way - this has thrown up the next thing to fix - i found some subnet definitions don't end up with a name (the IPs have no comment), I guess I can iterate over the subnets hash and substitute any undef comments with a value. Initially, I did a find/replace in the output CSV file, but it would be nice to resolve it in the code. I'll have a look into this.

    The demo program is really useful, thanks. I need to have a proper look at it, but my initial guess as to how the original code is working out the subnets is that rather than doing any de-duplication of subets or similar, it is just populating a subnet address and prefix into the hash and if the same one already exists, it just get overwritten.

    Thanks again, appreciate your help.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1040280]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (6)
As of 2017-09-20 17:12 GMT
Find Nodes?
    Voting Booth?
    During the recent solar eclipse, I:

    Results (237 votes). Check out past polls.