Need assistance modifying an existing PERL script to compute IP Address metrics

Posted on 2015-01-17
Last Modified: 2015-01-17
Hi Team,

   Just need some assistance modifying an existing PERL script to accommodate new fields I have to eventually import to our mysql database.

1. I have a raw file ip_search.csv  which contains a "Device_Name", 'IP_ADDRESS", "BASE_IP", "MASK"

[me@mypc]# cat ip_search.csv

Open in new window

2. After being processed by the PERL script below, the output now has  "Device_Name", 'IP_ADDRESS", "BASE_IP", "MASK", "START_IP_RANGE", "END_IP_RANGE", "START_DECIMAL", "END_DECIMAL"   where the START and END Decimals are the decimal equivalent of the IP addresses (INET_ATON function).

[me@mypc]# ./FirewallIPRange 

Open in new window

3. The PERL script that is doing that is this:
[me@mypc]# cat FirewallIPRange 

use strict;
use warnings;
use Net::IP;
use Socket;

open my $csv, '<', 'ip_search.csv' or die "failed to open 'ip_search.csv' $!";

while( my $record = <$csv>) {
    chomp $record;
    my($network, $cidr) = (split(/,/, $record))[-2,-1];
    my $lan = Net::IP->new("$network/$cidr");
    my @first = split(/\./, $lan->ip);
    my $first_ip = join('.', @first);
    my @last  = split(/\./, $lan->last_ip);
    my $last_ip = join('.', @last);
    my $first = unpack("N", inet_aton($first_ip) );
    my $last  = unpack("N", inet_aton($last_ip) );
    print join(',', $record, $first_ip, $last_ip, $first, $last), "\n";
close $csv;

Open in new window

4. My new customer now sends me a raw file which contains less info, (base IP is missing)  but still needs similar fields (with a couple extra) as the output:

a. New source files looks like this  --> "Device_Name", 'IP_ADDRESS", "MASK"

[me@mypc]# cat ip_search_new.csv

Open in new window

b. but the resulting csv output would need to look like this:


[me@mypc]# ./FirewallIPRangeNEW 

Open in new window

Would appreciate if someone could just help me modify the existing PERL script listed above so the new version can also compute the BASE IP and the CIDR value, based on initial IP address and mask?

Thanks very much.
Question by:rleyba828
LVL 84

Accepted Solution

ozo earned 500 total points
ID: 40555775
while( my $record = <$csv>) {
    chomp $record;
    my($network, $cidr) = (split(/,/, $record))[-2,-1];
    my $mask=-(1<<(32-$cidr));
    my $mask_ip=join('.',unpack'C4',pack"N",$mask);
    my $first = unpack("N", inet_aton $network );
    my $last = $first|~$mask;
    my $last_ip = join('.',unpack'C4',pack"N",--$last);
    $first &= $mask;
    my $first_ip = join('.', unpack'C4',pack"N",++$first);
    print join(', ', $record, $mask_ip, $first_ip, $last_ip, $first, $last), "\n";

Author Comment

ID: 40555836

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
html form to write data to csv 24 132
Need script to search multiple files in one drive 13 55
Perl Snippet to Parse String 1 22
Check for Linux process in script 7 45
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (,  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…

792 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question