If I want to have IP addresses like: 1.1.1.0/8 or 203.10.22.19 to 203.10.22.255 is there any shortcut way to put this in the @array?
1.1.1.0/8 is not a valid network address, by the way. Perhaps you want 1.0.0.0/8?
Here's a way to produce a list of all IP addresses in a network, given it's subnet address and mask (in the form a.b.c.d/e)
sub get_ips
{
my $subnet_specification=shift; # Get t
+he subnet specification.
my ($network, $mask)=split(/\//, $subnet_specification);
+ # Break it into a subnet and mask.
my @subnet_array=split/\./,$network; # Spli
+t the network into individual octets.
my $subnet_value=$subnet_array[0]*0x1000000+$subnet_array[1]*0x100
+00+$subnet_array[2]*0x100+$subnet_array[3];
# $subnet_value now contains a 32-bit value corresponding to the n
+etwork address given.
# Now calculate the top end of the subnet.
my $top_address=$subnet_value+(2**(32-$mask)-1);
# We now have the first and last addresses required. Produce a lis
+t of all addresses in between, in dotted-decimal format.
my @return_value; # Holds the array
+ of addresses we are returning
for (my $address=$subnet_value; $address<=$top_address; $address++
+)
{
my @octets=(int($address/0x1000000)%0x100, int($address/0x1000
+0)%0x100, int($address/0x100)%0x100, $address%0x100);
push(@return_value, join(".", @octets)); #
+Assemble it into a dotted-decimal form and add it to the returned arr
+ay.
}
return @return_value;
}
Just call get_ips("192.168.0.0/24");
|