The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

 Net::Kalk - Perl extension for calculate addresses and networks IP
 

SYNOPSIS

 use Net::Kalk;

 Functions are not exported , it is necessary to use full name to call them
 Example : Net::Kalk::network($add,$mask);

FUNCTIONS AND PARAMETERS

1 : Address

 plus    ($x,$add);  #  calculation of $add + $x , $x is a decimal value
 minus   ($x,$add);  #  calculation of $add - $x , $x is a decimal value
 invers  ($add);     #  calculation of the complement to 1 ip address 

 examples :
  - plus   ('1','1.2.3.255') -> 1.2.4.0
  - plus   ('10','1.2.3.4')  -> 1.2.3.14
  - minus  ('1','1.2.4.0')   -> 1.2.3.255
  - minus  ('10','1.2.3.14') -> 1.2.3.4
  - invers ('255.255.0.0')   -> 0.0.255.255
  - invers ('0.255.255.255') -> 255.0.0.0

2 : Address conversion

 ip      ('src',$add);
 x12     ('src',$add);
 decimal ('src',$add);
 hexa    ('src',$add);
 binary  ('src',$add);

 src = ip or x12 or decimal or hexa or binary

 x12 = address ip without the points , in addition by 0 if a value is on 1 or 2 digits
 result AAABBBCCCDDD still on 12 digits
 possibility of direct numerical comparisons of addresses

 examples :
  - x12     ('ip','1.2.3.4')       -> 001002003004
  - decimal ('ip','1.2.3.4')       -> 16909060
  - hexa    ('ip','1.2.123.254')   -> 01.02.7B.FE
  - binary  ('ip','1.2.3.4')       -> 00000001.00000010.00000010.00000100
  - ip      ('x12','001002003004') -> 1.2.3.4
  - ip      ('decimal','16909060') -> 1.2.3.4
  - x12     ('decimal','16909060') -> 001002003004
  - hexa    ('decimal','16909060') -> 01.02.03.04

3 : Mask conversion

 mask($mask);
 mask($cidr);
 
 examples :
  - mask(24)            -> 255.255.255.0
  - mask(255.255.255.0) -> 24

4 : Network

 network   ($add,$mask);
 broadcast ($add,$mask);
 nb_add    ($add,$mask);  # number of network address
 nb_net    ($add,$mask);  # number of possible networks with the same mask
 net_all   ($add,$mask);  # all the calculations

 examples :
  - network   ('1.2.3.4','255.255.255.0') -> 1.2.3.0
  - broadcast ('1.2.3.4','255.255.255.0') -> 1.2.3.255
  - nb_add    ('1.2.3.4','255.255.255.0') -> 256
  - nb_net    ('1.2.3.4','255.255.255.0') -> 16777216 
  - net_all   ('1.2.3.4','255.255.255.0') -> 1.2.3.0 1.2.3.255 256 16777216

5 : Error

 error ('fct',$add); 

 fct = ip , mask , cidr or x12
  - ip   : ip address test
  - mask : mask test
  - cidr : cidr test
  - x12  : ip test in x12 format

 return :
  - 1 if error
  - 0 if ok

 examples :
  - error ('ip','1.2.3.4')         -> 0
  - error ('ip','1.2.3.300')       -> 1
  - error ('mask','255.255.255.0') -> 0
  - error ('mask','1.2.3.4')       -> 1
  - error ('cidr','24')            -> 0
  - error ('x12','172030064156')   -> 0

6 : Address range

 range ($start,$end);

 result  : search for networks necessary to cover an address range defined by a start and an end
 display : network    broadcast   mask     cidr   wildcard   number_of_addresses

 examples :
  - range ('0.0.0.1','255.255.255.254')

7 : Sorting a list by an IP address column

 sort ($list,$options);

 list    = list of addresses or line to sort , separated by semicolons or by Line Feed
 options = Cru with 
  C = number of column to sort on , default = 1
  r = for descending order i      , default = ascending order
  u = for uniques lines only      , default = for all lines 

 examples :
 $list  = "ip route 5.0.0.0 255.0.0.0 192.168.5.254\n";
 $list .= "ip route 3.0.0.0 255.0.0.0 192.168.99.254\n";
 $list .= "ip route 11.0.0.0 255.0.0.0 192.168.45.254\n";
 $list .= "ip route 2.0.0.0 255.0.0.0 172.32.21.254\n";
 $list .= "ip route 1.0.0.0 255.0.0.0 10.69.14.254\n";
 $list .= "ip route 23.0.0.0 255.0.0.0 192.168.0.254\n";
 $list .= "ip route 2.0.0.0 255.0.0.0 172.32.21.254\n";
 $list .= "ip route 8.0.0.0 255.0.0.0 10.31.21.254\n";
 $list .= "ip route 210.0.0.0 255.0.0.0 172.16.78.254\n";
 
  - sort ($list)
  - sort ($list,'3')
  - sort ($list,'3r')
  - sort ($list,'3u')
  - sort ($list,'3ru')

8 : Address and network summary

 summary ($list);
 summary ($list,'d');

 list    = list of addresses and networks to summarize , separated by semicolons or by Line Feed
 network = address + mask in short or long format , or address + wildcard mask
 examples : A.B.C.D/24 ou A.B.C.D 255.255.255.0 ou A.B.C.D 0.0.0.255

 with a second parameter at 'd' , the calculation is displayed in detail, with all the intermediate steps

 examples :
  - summary ('10.145.23.89;172.10.35.0 255.255.255.0;172.10.35.100 0.0.0.15;192.168.25.137/24;172.10.35.22;172.10.35.241/28')
  - summary ('10.145.23.89;172.10.35.0 255.255.255.0;172.10.35.100 0.0.0.15;192.168.25.137/24;172.10.35.22;172.10.35.241/28' , 'd')

9 : Address and network included

 included ($list);
 included ($list,'d');

 list    = list of addresses and networks to include , separated by semicolons or by Line Feed
 network = address + mask in short or long format , or address + wildcard mask
 examples : A.B.C.D/24 ou A.B.C.D 255.255.255.0 ou A.B.C.D 0.0.0.255

 with a second parameter at 'd' , the calculation is displayed in detail, with all the intermediate steps

 examples :
  - included ('10.145.23.89;10.145.22.0 255.255.255.0;10.145.24.0/24')
  - included ('10.145.23.89;10.145.22.0 255.255.255.0;10.145.24.0/24' , 'd')

UTILIZATION EXAMPLE

 ip_kalk : example of script perl using Net::Kalk which can call all functions
 command : ip_kalk FUNCTION [VARIABLES] 
 example : ip_kalk decimal ip 1.2.3.4

   #!/usr/bin/perl -w

   exit if @ARGV == 0;
   $fct = $ARGV[0];

   $all_fct  = "error|plus|minus|invers";
   $all_fct .= "|ip|x12|decimal|hexa|binary|mask";
   $all_fct .= "|network|broadcast|nb_add|nb_net|net_all";
   $all_fct .= "|range|sort|summary|included";
   exit if $fct !~ /^($all_fct)$/;

   shift;
   use Net::Kalk;
   $fct = "Net::Kalk::$fct";

   $result = &$fct(@ARGV);
   print $result if defined($result);

VERSION AND AUTHOR

 Version  : v2.08 - April 2021
 Author   : Thierry Le Gall
 Contact  : facila@gmx.fr