I bumped my head on this piece of code for two days without any success: What I need to do is obtain a list of IP addresses starting from a long list of NETWORKS in this format:
NETWORK NUMBER_OF_HOSTS
192.168.1.0 512
I've found this bunch of code in the Net:IP module that seems perfect for my needs:
my $ip = new Net::IP ('192.168.1.0 + 512');
do {
print $ip->ip(), "\n";
} while (++$ip);
With the NETWORK and HOST value declared explicitly the snippet works flawlessly, when I use it inside a while loop passing the data through variables, the problems start.
After many tries I've modified the list as follows:
'192.168.150.0 + 512'
And this is the resulting script:
#!/usr/bin/perl
use strict;
use warnings;
use Net::IP;
open(MYFILE, "ips.txt") or die "$0: Can't open input file $input: $!\n";
while (<MYFILE>){
chomp $_;
my $ip = new Net::IP ($_);
do {
print $ip->ip(), "\n";
} while (++$ip);
}
close(MYFILE);
When I try to execute it gives me the following error:
Can't call method "ip" on an undefined value at ./iplist.pl line 14, line 1.
If I print the variable inside the while loop with this print $_,"\n"; it prints the variable correctly, so where am I wrong? Any help is appreciated.
UPDATE1: od output for ips.txt
0000000 ' 4 1 . 5 7 . 1 1 6 . 0 + 1
0000020 0 2 4 ' \n ' 4 1 . 5 7 . 1 9 2 .
0000040 0 + 1 6 3 8 4 ' \n ' 4 1 . 6
0000060 6 . 1 9 2 . 0 + 1 6 3 8 4 '
0000100 \n ' 4 1 . 7 4 . 8 0 . 0 + 4
0000120 0 9 6 ' \n ' 4 1 . 7 5 . 4 8 . 0
0000140 + 4 0 9 6 ' \n ' 4 1 . 7 6 .
0000160 2 4 . 0 + 2 0 4 8 ' \n ' 4 1
0000200 . 7 7 . 6 4 . 0 + 2 0 4 8 '
0000220 \n ' 4 1 . 7 8 . 4 0 . 0 + 1
0000240 0 2 4 ' \n
od -c ips.txt?