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 it’s 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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |