As a part of a project, I need to manipulate an IP address and network mask, both in dotted notation, to give a network address in slash notation (i.e. 192.168.1.5 255.255.255.0 becomes 192.168.1.0/24)
I've written the sub shown below, but I don't know if my method for finding the number of bits in the netmask is the best way to do it.......
sub nets {
# takes 2 args - IP address and network mask
my $ip = $_[0];
my $mask = $_[1];
my $nets = inet_ntoa ((inet_aton($ip)) & (inet_aton($mask)));
my $binmask = unpack ("B32", pack ("C4", split(/\./, $mask)));
$binmask =~ /(10)/;
my $bits = $-[1]+1;
my $network = "$nets\/$bits";
return $network;
}