Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid

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

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!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • 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
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            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?

    What's my password?
    Create A New User
    [Corion]: So, it's not all bad with the new FF :)
    [holli]: 2factor is a bit too much for me. i just use passwords like jhbn8789q27312laks dmaw98^
    [moritz]: isn't there also some about:config setting to enable the old plugin API?
    [Discipulus]: indeed, only the logo is nicer
    [Corion]: moritz: Hmm, I doubt so, as I think they moved away from XUL and other UI stuff, which was used by most plugins I used
    [Corion]: holli: I'm trying it out with Github and so far I don't dislike it. It's basically like an extra key I press to log in, which is an OK flow for Github
    [Corion]: I haven't tried out recovery in case of a broken USB key though (even though I have three such U2F tokens lying around here)
    [oiskuu]: randomly banging on the keyboard yields pretty poor entropy. much better to just base64 some /dev/random
    moritz has a pass entry with recovery codes
    [oiskuu]: Corion, what version was your old FF and what reason for upgrade?

    How do I use this? | Other CB clients
    Other Users?
    Others rifling through the Monastery: (11)
    As of 2017-11-20 20:23 GMT
    Find Nodes?
      Voting Booth?
      In order to be able to say "I know Perl", you must have:

      Results (293 votes). Check out past polls.