Friday, 15 March 2019

Perl script to GetSubnetAddressMaskIP ( receives the subnet mask in the format 192.168.1.0/24 and return in the format 192.168.1.0 255.255.255.0)




sub GetSubnetAddressMaskIP
  {
     my $subnet = trim(<input>);
   
if($subnet =~ m/^(\d\d?\d?)\.(\d\d?\d?)\.(\d\d?\d?)\.(\d\d?\d?)\/([0-9]{1,2})$/ && ( $1 <= 255 && $2 <= 255 && $3 <= 255 && $4 <= 255 && $5 <= 32))
   {   
         my $netbit=$5;
my ($subnetIP,$maskValue) = split('/',$subnet);

if($netbit <0 or $netbit >32)
   {
return -1;
   }
 
my $mask  = (2 ** $netbit - 1) << (32 - $netbit);
my $netmask = join( '.', unpack( "C4", pack( "N", $mask ) ) );

if(ValidateIPAddress($netmask)==1)
   {
  return "$subnetIP $netmask";
   }
else
   {
  return -1;
   }

       }
elsif($subnet =~ m/^(\d\d?\d?)\.(\d\d?\d?)\.(\d\d?\d?)\.(\d\d?\d?)\s+(\d\d?\d?)\.(\d\d?\d?)\.(\d\d?\d?)\.(\d\d?\d?)$/ && ( $1 <= 255 && $2 <= 255 && $3 <= 255 && $4 <= 255 && $5 <= 255 && $6 <= 255 && $7 <= 255 && $8 <= 255))
   {
     return $subnet;
   }
  else
   {
     return -1;
   }
  }

No comments:

Post a Comment

Git

1 git add ↳ It lets you add changes from the working directory into the staging area 2 git commit ↳ It lets you save a snapshot of currently...