dotowwxo has asked for the wisdom of the Perl Monks concerning the following question:
Basically what I am trying to do is:
1. I have a list of IP address in testingIP.txt
2. I want to loop through the text file and find out which IP address is in the net mask that I have specified using a PERL module that I have installed -- Net::Netmask http://search.cpan.org/~muir/Net-Netmask-1.9015/Netmask.pod
Unfortunately, it only works when there is only 1 IP address in testingIP.txt, this is my output for my current code:
Use of uninitialized value $i in bitwise and (&) at /usr/local/share/p +erl5/site_perl/5.26/Net/Netmask.pm line 411, <$fh> line 1. IP address: 192.168.1.1 is not inside 192.168.1.0/27 Use of uninitialized value $i in bitwise and (&) at /usr/local/share/p +erl5/site_perl/5.26/Net/Netmask.pm line 411, <$fh> line 2. IP address: 192.168.1.2 is not inside 192.168.1.0/27 Use of uninitialized value $i in bitwise and (&) at /usr/local/share/p +erl5/site_perl/5.26/Net/Netmask.pm line 411, <$fh> line 3. IP address: 192.168.1.3 is not inside 192.168.1.0/27 IP address: 192.168.1.4 is inside 192.168.1.0/27
This is my code
#!/usr/bin/perl use strict; use warnings; use Net::Netmask; my $filename = 'testingIP.txt'; open(my $fh, '<:encoding(UTF-8)', $filename) or die "Could not open file '$filename' $!"; my $block = Net::Netmask->new("192.168.1.0/27"); while (my $row = <$fh>){ my @row = $row; for (my $i=0; $i < scalar(@row); $i++){ if ($block->match($row[$i])){ print "IP address: $row[$i] is inside $block\n"; } else{ print "IP address: $row is not inside $block\n"; } } }
|
|---|