Solved

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

Posted on 2015-01-17
2
204 Views
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
Firewall_A,10.1.40.3,10.1.40.0,24
Firewall_B,10.8.40.1,10.8.40.0,24
Firewall_C,10.8.31.3,10.8.31.0,28
Firewall_D,10.8.31.1,10.8.31.0,28

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 
Firewall_A,10.1.40.3,10.1.40.0,24,10.1.40.1,10.1.40.254,167847937,167848190
Firewall_B,10.8.40.1,10.8.40.0,24,10.8.40.1,10.8.40.254,168306689,168306942
Firewall_C,10.8.31.3,10.8.31.0,28,10.8.31.1,10.8.31.14,168304385,168304398
Firewall_D,10.8.31.1,10.8.31.0,28,10.8.31.1,10.8.31.14,168304385,168304398

Open in new window


3. The PERL script that is doing that is this:
[me@mypc]# cat FirewallIPRange 
#!/usr/bin/perl

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);
    $first[-1]++;
    my $first_ip = join('.', @first);
    
    my @last  = split(/\./, $lan->last_ip);
    $last[-1]--;
    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
Firewall_A,10.1.40.3,24
Firewall_B,10.8.40.1,24
Firewall_C,10.8.31.3,28
Firewall_D,10.8.31.1,28

Open in new window


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

 "Device_Name", 'IP_ADDRESS", "BASE_IP", "MASK",  "MASK_CIDR", "START_IP_RANGE", "END_IP_RANGE", "START_DECIMAL", "END_DECIMAL"

[me@mypc]# ./FirewallIPRangeNEW 
Firewall_A,10.1.40.3,10.1.40.0,24,255.255.255.0,10.1.40.1,10.1.40.254,167847937,167848190
Firewall_B,10.8.40.1,10.8.40.0,24,255.255.255.0, 10.8.40.1,10.8.40.254,168306689,168306942
Firewall_C,10.8.31.3,10.8.31.0,28,255.255.255.240,10.8.31.1,10.8.31.14,168304385,168304398
Firewall_D,10.8.31.1,10.8.31.0,28,255.255.255.240, 10.8.31.1,10.8.31.14,168304385,168304398

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.
0
Comment
Question by:rleyba828
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 84

Accepted Solution

by:
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";
}
0
 

Author Comment

by:rleyba828
ID: 40555836
Perfect!
0

Featured Post

Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Script to change all permissions to my user 7 51
sqlldr issue 1 26
AWK: Pythagoras script 6 37
while read ID; do 4 58
Utilizing an array to gracefully append to a list of EmailAddresses
In threads here at EE, each comment has a unique Identifier (ID). It is easy to get the full path for an ID via the right-click context menu. However, we often want to post a short link within a thread rather than the full link. This article shows a…
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…

726 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