user1236307
user1236307

Reputation: 13

Text in argument on perl code with CIDR ip addresses

My program verifies if ip addresses from an input.txt like this:

190.116.16.99
200.48.11.235
200.11.48.141
190.50.145.4
170.40.111.178
186.160.42.12
200.1.176.15
200.1.177.54
200.1.178.253
200.1.179.254
200.1.184.132

are inside a bunch of CIDR IP Blocks, i use the module Net::IP::Match to help me out:

IP's that are not inside those blocks are relegated to an output.txt.

I need help programming how to make an ipblocks.txt like this:

161.132.0.0/16
181.64.0.0/16
181.65.0.0/16
181.176.0.0/16
186.160.0.0/14
190.8.128.0/19
190.11.208.0/20
190.12.64.0/20
190.12.80.0/20
190.40.0.0/17
190.40.128.0/17
190.41.0.0/17
190.41.128.0/17
190.42.0.0/16
190.43.0.0/16
190.52.112.0/20
190.81.0.0/18
190.81.64.0/18
190.81.128.0/17
190.102.128.0/19
190.105.240.0/20
190.108.80.0/22
190.108.84.0/22
190.108.88.0/22
190.108.92.0/22
190.113.192.0/19
190.114.248.0/22
190.116.0.0/16
190.117.0.0/16
190.118.0.0/16
190.124.48.0/22
190.124.52.0/22
190.187.0.0/17
190.187.128.0/17
190.222.0.0/16
190.223.0.0/16
190.232.0.0/16
190.233.0.0/16
190.234.0.0/16
190.235.0.0/16
190.236.0.0/16
190.237.0.0/16
190.238.0.0/16
190.239.0.0/16
192.102.84.0/24
200.0.107.0/24
200.0.110.0/24
200.0.118.0/23
200.0.136.0/24
200.0.142.0/24
200.0.164.0/24
200.0.165.0/24
200.0.166.0/23
200.0.168.0/24
200.0.169.0/24
200.0.170.0/23
200.0.172.0/23
200.1.176.0/22
200.1.180.0/22
200.3.114.0/24
200.3.241.0/24
200.3.242.0/24
200.4.192.0/18
200.10.64.0/19
200.10.130.0/23
200.10.188.0/24
200.10.250.0/24
200.11.32.0/24
200.11.34.0/23
200.11.36.0/24
200.11.37.0/24
200.11.38.0/23
200.11.48.0/20
200.16.0.0/22
200.16.4.0/22
200.24.160.0/19
200.31.96.0/20
200.31.112.0/20
200.37.0.0/16
200.48.0.0/16
200.60.0.0/17
200.60.128.0/18
200.60.192.0/19
200.60.224.0/19
200.62.128.0/18
200.62.192.0/20
200.62.208.0/20
200.62.224.0/19
200.89.0.0/20
200.89.16.0/20
200.106.0.0/18
200.106.64.0/18
200.107.128.0/18
200.108.96.0/20
200.110.0.0/19
200.110.32.0/20
200.121.0.0/17
200.121.128.0/17
200.123.0.0/19
201.230.0.0/17
201.230.128.0/17
201.240.0.0/17
201.240.128.0/17
206.223.130.0/24
209.45.0.0/17
216.244.128.0/18

can be inside in the module's argument, instead of having lines and lines of code about those blocks.

Here is my code.

unlink ("output.txt");
open (INPUT, 'input.txt');
 while (<INPUT>) {
    chomp;
    use Net::IP::Match::Regexp qw( create_iprange_regexp match_ip ); 
my $re1 = create_iprange_regexp(
'161.132.0.0/16',
'181.64.0.0/16',
'181.65.0.0/16',
'181.176.0.0/16',
'186.160.0.0/14',
'190.8.128.0/19',
'190.11.208.0/20',
'190.12.64.0/20',
'190.12.80.0/20',
'190.40.0.0/17',
'190.40.128.0/17',
'190.41.0.0/17',
'190.41.128.0/17',
'190.42.0.0/16',
'190.43.0.0/16',
'190.52.112.0/20',
'190.81.0.0/18',
'190.81.64.0/18',
'190.81.128.0/17',
'190.102.128.0/19',
'190.105.240.0/20',
'190.108.80.0/22',
'190.108.84.0/22',
'190.108.88.0/22',
'190.108.92.0/22',
'190.113.192.0/19',
'190.114.248.0/22',
'190.116.0.0/16',
'190.117.0.0/16',
'190.118.0.0/16',
'190.124.48.0/22',
'190.124.52.0/22',
'190.187.0.0/17',
'190.187.128.0/17',
'190.222.0.0/16',
'190.223.0.0/16',
'190.232.0.0/16',
'190.233.0.0/16',
'190.234.0.0/16',
'190.235.0.0/16',
'190.236.0.0/16',
'190.237.0.0/16',
'190.238.0.0/16',
'190.239.0.0/16',
'192.102.84.0/24',
'200.0.107.0/24',
'200.0.110.0/24',
'200.0.118.0/23',
'200.0.136.0/24',
'200.0.142.0/24',
'200.0.164.0/24',
'200.0.165.0/24',
'200.0.166.0/23',
'200.0.168.0/24',
'200.0.169.0/24',
'200.0.170.0/23',
'200.0.172.0/23',
'200.1.176.0/22',
'200.1.180.0/22',
'200.3.114.0/24',
'200.3.241.0/24',
'200.3.242.0/24',
'200.4.192.0/18',
'200.10.64.0/19',
'200.10.130.0/23',
'200.10.188.0/24',
'200.10.250.0/24',
'200.11.32.0/24',
'200.11.34.0/23',
'200.11.36.0/24',
'200.11.37.0/24',
'200.11.38.0/23',
'200.11.48.0/20',
'200.16.0.0/22',
'200.16.4.0/22',
'200.24.160.0/19',
'200.31.96.0/20',
'200.31.112.0/20',
'200.37.0.0/16',
'200.48.0.0/16',
'200.60.0.0/17',
'200.60.128.0/18',
'200.60.192.0/19',
'200.60.224.0/19',
'200.62.128.0/18',
'200.62.192.0/20',
'200.62.208.0/20',
'200.62.224.0/19',
'200.89.0.0/20',
'200.89.16.0/20',
'200.106.0.0/18',
'200.106.64.0/18',
'200.107.128.0/18',
'200.108.96.0/20',
'200.110.0.0/19',
'200.110.32.0/20',
'200.121.0.0/17',
'200.121.128.0/17',
'200.123.0.0/19',
'201.230.0.0/17',
'201.230.128.0/17',
'201.240.0.0/17',
'201.240.128.0/17',
'206.223.130.0/24',
'209.45.0.0/17',
'216.244.128.0/18');
   if (match_ip($_, $re1) == undef) {
open (OUTPUT, '>>output.txt');
 print OUTPUT "$_\n";
 close (OUTPUT); 
}
 }
 close (INPUT);

Thanks

PD: I need help making this script to request the txt's path, and make this script and exe file, can it be done?

Upvotes: 0

Views: 525

Answers (1)

Karsten S.
Karsten S.

Reputation: 2391

If I understand it correctly, you want to add a parameter to your script giving you a filename of an ip-range-txt file - or eventually put the name statically into the script, but load ip-ranges from that file.

Giving a filename is always a good idea. You may use Getopt::Long for this - it can handle input file, iprange file and output filename, but that's a different issue.

use autodie; # always a good idea especially because you haven't checked return values of 'open'.

my $ipranges_filename = $ARGV[0]; # or static filename

# load file with ip ranges
my @ipranges;
open( my $ip_range, "<", $ipranges_filename );
while(<$ip_range>) {
    chomp;
    push @ipranges, $_;
}
close($ip_range);

my $re1 = create_iprange_regexp( @ipranges );
# ... continue

Hope it helps. If not, give more precise input on your problem.

Upvotes: 2

Related Questions