User Contributed Perl Documentation IP::Address(3) NNNNAAAAMMMMEEEE IP::Address - Manipulate IP Addresses easily SSSSYYYYNNNNOOOOPPPPSSSSIIIISSSS use IP::Address qw($Use_CIDR_Notation $Always_Display_Mask); # Initialization of IP::Address objects my $ip = new IP::Address "10.0.0.1"; my $subnet = new IP::Address("10.0.0.0", "255.255.255.0"); my $othersubnet = new IP::Address("10.0.0.0", "24"); my $yetanothersubnet = new IP::Address "10.0.0.0/24"; # A string representation of an address or subnet print "My ip address is ", $ip->to_string, "\n"; # Just the string or the mask part... print "My ip address alone is ", $ip->addr_to_string, "\n"; print "and my netmask is ", $ip->mask_to_string, "\n"; # Enumeration of all the addresses within a given subnet, keeping # the original mask my @hosts = $subnet->enum; for $i (@hosts) { print "address ", $i->to_string, " belongs to subnet ", $subnet->to_string, "\n"; } # You can also produce the list of host addresses in a given subnet my @hosts = $subnet->host_enum; for $i (@hosts) { print "Host ", $i->to_string, " is in subnet ", $subnet->to_string, "\n"; } # This calculates network and broadcast addresses for a subnet my $network = $subnet->network; my $broadcast = $subnet->broadcast; print "Subnet ", $subnet->to_string, " has broadcast address ", $broadcast->to_string, " and network number ", $network->to_string, "\n"; # Checks to see if a host address or subnet is contained within another # subnet if ($subnet->contains $ip) { print "Host ", $ip->to_string, " is contained in ", $subnet->to_string, "\n"; } # Masks and address components can be copied from object to object $ip1->set_addr($ip2); $ip1->set_mask($subnet); 12/Jul/99 Last change: perl 5.005, patch 02 1 User Contributed Perl Documentation IP::Address(3) # Ammount of hosts in a subnet can also be easily calculated $max_hosts_in_subnet = $subnet->how_many - 2; # A range of IP Addresses @range = $ip->range($final_ip); # From $ip to $final_ip @range = $ip->range(@dont_know_which_is_larger); # From the smallest on the list + $ip to # the largest DDDDEEEESSSSCCCCRRRRIIIIPPPPTTTTIIIIOOOONNNN This module provides a simple interface to the tedious bit manipulation involved when handling IP address calculations. It also helps by performing range comparisons between subnets as well as other frequently used functions. Most of the primitive functions return an IP::Address object. The variables $$$$UUUUsssseeee____CCCCIIIIDDDDRRRR____NNNNoooottttaaaattttiiiioooonnnn and $$$$AAAAllllwwwwaaaayyyyssss____DDDDiiiissssppppllllaaaayyyy____MMMMaaaasssskkkk affect how the ->to_string function will present its result. The names are hopefully intuitive enough. Note that IP addresses are not properly compacted (ie, 200.44.0/18 is written as 200.44.0.0/18) because this adapts to the widely adopted but incorrect notation. Perhaps a later version will include a variable to change this. This code has not been widely tested yet. Endianness problems might very well exist. Please email the author if such problems are found. This software is (c) Luis E. Munoz. It can be used under the terms of the perl artistic license provided that proper credit is preserved and that the original documentation is not removed. This software comes with the same warranty as perl itself (ie, none), so by using it you accept any and all the liability. AAAAUUUUTTTTHHHHOOOORRRR Luis E. Munoz SSSSEEEEEEEE AAAALLLLSSSSOOOO _p_e_r_l(1). 12/Jul/99 Last change: perl 5.005, patch 02 2 User Contributed Perl Documentation IP::Address(3) 12/Jul/99 Last change: perl 5.005, patch 02 3