Beefy Boxes and Bandwidth Generously Provided by pair Networks RobOMonk
XP is just a number
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??

First post! I have inherited a script which is working fine, but I now need to include another variable in the output the script is producing. My Perl ability is pretty basic and my main problem is understanding what some of the existing code does in order to be able to modify it to do what I need - I believe the relevant code is below

# Process a line and split out the data into variables my $function = sub { my ($arr) = @_; my ( $ipaddress, $prefix, $ripe, $interface, $device, $location, $comment ) = @$arr; s/^\s+|\s+$//g for ( $ipaddress, $prefix, $interface, $device, $location, $comment ); if ( is_ipv4($ipaddress) ) { store_subnet( $ipaddress, $prefix ) if $prefix;
# Produce IP subnets from the list of IPs sub store_subnet { my ( $ip, $len ) = @_; my $ipn = unpack "N", Socket::inet_aton $ip; $len =~ s/^\///; my $maskn = 0xFFFFFFFF << ( 32 - $len ); $ipn = $ipn & $maskn; $ip = Socket::inet_ntoa pack "N", $ipn; $subnets{"$ip,$len"} = { ipn => $ipn, maskn => $maskn }; }
# Print it out print "subnet,prefix\n"; print "$_\n" for sort { $subnets{$a}{ipn} <=> $subnets{$b}{ipn} } keys %subnets;

So, what does the script currently do - it takes an excel spreadsheet which contains IP address allocations and generates a CSV file with two outputs types, (i) All the IP address allocations, with the output using the headers: "ipaddress,prefix,interface,device_name,location,comment"; (ii) A list of subnets, derived from the IP addresses, output using the headers: "subnet,prefix".

What do I want it now to do? Include a subnet name in the subnet list output, so effectively: "subnet,prefix,name".

My thought was to grab the $comment value from the IPs and use one of these to populate the subnet name field. As there are typically multiple IPs in a subnet, I was thinking just grab the first comment that is populated and use that (not all IPs have the comment field populated). My difficulty is I don't really understand well enough the code that takes the individual IP addresses and then derives the IP subnet address for these IPs - I know it's the second code snippet above - perhaps something like below (but I know this is not checking if wev've already grabbed a defined $comment?):

sub store_subnet { my ( $ip, $len, $comment ) = @_; my $ipn = unpack "N", Socket::inet_aton $ip; $len =~ s/^\///; my $maskn = 0xFFFFFFFF << ( 32 - $len ); $ipn = $ipn & $maskn; $ip = Socket::inet_ntoa pack "N", $ipn; $subnets{"$ip,$len,$comment"} = { ipn => $ipn, maskn => $maskn, na +me => $comment }; }

I think its straight forward to carry the $comment field into the store_subnet subroutine for each IP:

store_subnet( $ipaddress, $prefix, $comment ) if $prefix;

But I'm not sure how to use the comment in the store_subnet subroutine, and effectivley assign the first populated $comment to the derived subnet address

Then, to print it out I think it's a small tweak to jsut add the "name" header:

print "subnet,prefix,name\n"; print "$_\n" for sort { $subnets{$a}{ipn} <=> $subnets{$b}{ipn} } keys %subnets;

Apologies for the ramblings - appreciate any guidance offered to give me the desired outcome for this script and also improve my Perl ability!


In reply to How to include a variable in output for derived subnets by stroke

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others browsing the Monastery: (6)
    As of 2014-04-21 08:44 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      April first is:







      Results (492 votes), past polls