Net::IP::Util - Common useful routines like converting decimal address to binary and vice versa, determining address class, determining default mask, subnets and hosts and broadcast addresses for hosts in subnet.
use Net::IP::Util; ## subroutines isClassAddrA-E, bin2decIpAddr, dec2binIpAddr use Net::IP::Util qw/:class/; ## subroutines isClassAddrA-E, getAddrClass use Net::IP::Util qw/:convert/; ## subroutines bin2decIpAddr, dec2binIpAddr use Net::IP::Util qw/getAddrMaskDefault getAddrClass isValidMask extendMaskByBits calcSubnet calcSubnetCIDR calcSubnetExt getNetworkAddr ## Explicit inclusions isClassAddrA('127.0.32.45'); isClassAddrA('00001111.11110010.00100100.10000001'); dec2binIpAddr('128.0.0.56'); bin2decIpAddr('10001000.10100001.00010101.00000001'); getAddrMaskDefault('124.45.0.0'); getAddrMaskDefault('10000000.00000001.01010101.10000001'); getAddrClass('124.45.0.0'); getAddrClass('00001111.11110010.00100100.10000001'); isValidMask('255.255.252.0'); isValidMask('11111111.00000000.00000000.00000000'); extendMaskByBits('255.255.0.0',2); extendMaskByBits('11111111.00000000.00000000.00000000',2); calcSubnet('128.8.9.0'); calcSubnet('10001000.10100001.00010101.00000001'); calcSubnetCIDR('128.9.0.218/24'); calcSubnetCIDR('128.9.0.218/28', '255.255.255.0'); calcSubnetExt('128.0.0.1',4); calcSubnetExt('10001000.10100001.00010101.00000001',4); getNetworkAddr('198.23.16.0','255.255.255.240','255.255.255.252'); getNetworkAddr('198.23.16.0','255.255.255.240','255.255.255.252', 1); getNetworkAddr('10000000.00000001.01010101.10000001', '11111111.11111111.11111111.11110000', '11111111.11111111.11111111.11111100',); getNetworkAddr('10000000.00000001.01010101.10000001', '11111111.11111111.11111111.11110000', '11111111.11111111.11111111.11111100', 1);
This module tries provide the basic functionalities related to IPv4 addresses. Address class, subnet masks, subnet addresses, broadcast addresses can be deduced using the given methods. Ip addresses passed are also validated implicitly. Provision has been given to specify IP addresses in either dotted decimal notation or dotted binary notation, methods have been provided for conversion to-from these to notations which are internally used by other methods too.
isClassAddrA(<addr in decimal/binary>) : returns 1 if true eg. isClassAddrA('127.0.32.45'); isClassAddrA('00001111.11110010.00100100.10000001');
dec2binIpAddr(<ip addr in dotted decimal notation>) : returns ip in binary dotted notation eg. dec2binIpAddr('128.0.0.56');
bin2decIpAddr(<ip addr in dotted binary notation>) : returns ip in decimal dotted notation eg. bin2decIpAddr('10001000.10100001.00010101.00000001');
getAddrMaskDefault(<ip addr in decimal/binary notation>) : returns default subnet mask in dotted decimal notation eg. getAddrMaskDefault('124.45.0.0'); >> 255.0.0.0 getAddrMaskDefault('10000000.00000001.01010101.10000001'); >> 255.0.0.0
getAddrClass(<ip addr in decimal/binary notation>) : returns class (A/B/C/D/E) of ip address eg. getAddrClass('124.45.0.0'); getAddrClass('00001111.11110010.00100100.10000001');
isValidMask(<ip addr in decimal/binary notation>) : returns 1 if valid mask eg. isValidMask('255.255.252.0'); isValidMask('11111111.00000000.00000000.00000000');
extendMaskByBits(<ip addr in decimal/binary notation>,<no.of bits to extend>) : returns mask after extending/turning on given no. of bits after the already on bits of the mask eg. extendMaskByBits('255.255.0.0',2); >> 255.255.192.0 extendMaskByBits('11111111.00000000.00000000.00000000',2); >> 11111111.11000000.00000000.00000000
calcSubnet(<ip addr in decimal/binary notation>) : returns (no. of subnets, no. of hosts) calcSubnet('128.90.80.12'); calcSubnet('11000000.00000000.11000000.01011100'); - These always assumes Default Mask in calculation - hence no of subnets returned is always 0
calcSubnetCIDR(<ip addr in decimal/binary CIDR notation>, [<mask in decimal/binary notation>]) : returns (no. of subnets, no. of hosts) calcSubnetCIDR('128.87.56.26/28'); calcSubnetCIDR('128.87.56.26/28','255.255.252.0');
calcSubnetExt(ip addr in decimal/binary notation>, no. of bits to extend in default mask OR no. of borrowed bits) : returns (no. of subnets, no. of hosts) eg. calcSubnetExt('128.0.0.1',4); calcSubnetExt('10001000.10100001.00010101.00000001',4); Expln : no. of borrowed bits is added to the default subnet mask of ip addr to subnet mask and subnetting is done so : *************************************************** 127.0.40.1 = ip addr 255.0.0.0 = default subnet mask no. of borrowed bits = 4 => 255.240.0.0 = extended mask ***************************************************
getNetworkAddr(<ip addr in decimal/binary notation>, <default mask in decimal/binary notation>, <subnet mask in decimal/binary notation>, <true flag - if you want broadcast addresses instead of n/w addresses ) : returns network/broadcast addresses of the subnets after subnetting as a list eg. getNetworkAddr('198.23.16.0','255.255.255.240','255.255.255.252'); >> ('198.23.16.0','198.23.16.4','198.23.16.8','198.23.16.12') getNetworkAddr('198.23.16.0','255.255.255.240','255.255.255.252',1); >> ('198.23.16.3','198.23.16.7','198.23.16.11','198.23.16.15') getNetworkAddr('10000000.00000001.01010101.10000001', '11111111.11111111.11111111.11110000', '11111111.11111111.11111111.11111100',); >> Always returns n/w addresses in dotted decimal irrespective of binary/decimal address parameter passed
IPv4 only Validation of IP addresses are done, but because of conversions here and there it may not show the IP address properly in the error message as passed earlier by the user.
Net::IP, Net::IpAddr etc.
debashish@cpan.org
Copyright 2013 Debashish Parasar, all rights reserved.
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
To install Net::IP::Util, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Net::IP::Util
CPAN shell
perl -MCPAN -e shell install Net::IP::Util
For more information on module installation, please visit the detailed CPAN module installation guide.