++ed by:

2 PAUSE users

Joel C. Maslak


IP::Random - Generate IP Addresses Randomly


version 1.002


  use IP::Random qw(random_ipv4);

  my $ipv4 = random_ipv4();


This provides a random IP (IPv4 only currently) address, with some extensability to exclude undesired IPv4 addresses (I.E. don't return IP addresses that are in the multicast or RFC1918 ranges).

By default, the IP returned is a valid, publicly routable IP address, but this behavior can be adjusted.



Returns a random IPv4 address to the caller (as a scalar string - I.E. in the format ""). There are several named optional parameters available:


This allows replacement of the random number generator. By default, the generator used is:

  sub { int(rand(shift() + 1)) }

The code referenced passed as rand is called as a function with two arguments. The first argument is the maximum integer to generate (it must accept values up to at least 255). This will always be 255 when called by random_ipv4(), but is allowed to be specified to allow a generic routine to be used for future IPv4 and IPv6 address generation.

The second argument (which probably won't be used by most generators) is the octet number starting at 1, from the left to right.

  my $rand = sub { int( rand( ( shift() +1 ) / 2 ) * 2 ) };
  my $ipv4 = random_ipv4( rand => $rand );

The above code would return only even numbers for all 4 octets of the IPv4 address (this is probably not terribly useful).

If you want to modify various arguments, perhaps excluding IP addresses ending in .0 and 255, you could do something like:

  my $rand = sub {
    my ( $max, $octet ) = @_;

    if ( $octet == 3 ) {    # Last (least significant) Octet
      return int( rand( $max / 2 - 1 ) * 2 ) + 2;
    } else {
      return int( rand( shift() +1 ) );
  my $ipv4 = random_ipv4( rand => $rand );

This is an array reference of CIDRs (in string format) to exclude from the results. See default_exclude() for the default list, which excludes addresses such as RFC1918 (private) IP addresses. If passed an empty list reference such as [], it will not exclude any IPs. This is almost certainly not what you desire (since it may return IPs in class D and class E space - such as or

You might be better served by looking at additional_types_allowed.

By default, the default exclude list will include all IP addresses that can, with certainty, be considered non-global IP addresses - for instance, RFC1918 addresses. It may include IP addresses that are not actually on the internet, however. A use might be to exclude an organization's own internal IPs. In that case, you should take the default excludes and add an additional exclude:

  my $ipv4 = random_ipv4(
    exclude => [ default_exclude(), '' ] );

Of course this particular example can also be done with the additional_exclude optional parameter.

Note that exclude cannot be used with additional_types_allowed.


Adds a list of exclude items, similar to exclude, but without removing the default exclude list. See the exclude parameter above. Like the exclude parameter, this expects to be a list reference.

Example, to exclude a signle IP:

  my $ipv4 = rand_ipv4( additional_exclude => [ '' ] );

This is an array refence of strings that contain the "groups" you do not want to exclude by default. For instance, you may want to use some/all RFC1918 addresses.

Valid groups:


Limited broadcast address (


Basic protocol design (,,


Private-use networks (,,


Network interconnect device benchmark testing (


6to4 relay anycast (


Multicast (


Link local (


IETF protocol assignments (


Documentation Addresses (,,


Shared address space / Carrier NAT (

A typical use might be to include 10.x.x.x RFC1918 addresses among possible addresses to return. This example allows addresses in the 10.x.x.x range while continuing to exclude and

  my $ipv4 = random_ipvr(
    additional_types_allowed => [ 'rfc1918' ],
    additional_exclude       => [ '', '' ]

in_ipv4_subnet($subnet_cidr, $ip)

This is a helper function that tests whether an IP (passed as a string in the format "") is in a subnet passed in string CIDR notation (for instance, "").

Returns a true value if the IP is contained in the subnet, otherwise returns false.

Example, which returns a true value:

  if (in_ipv4_subnet('', '')) {
    say "Is loopback!";


Returns the default exclude list for IPv4, as a list reference containing CIDR strings.

Additional CIDRs may be added to future versions, but in no case will standard Unicast publicly routable IPs be added.

This list contains:

"This" Network (RFC 1122, Section

Private-Use Networks (RFC1918)

Shared Address Space (RFC6598)

Loopback (RFC 1122, Section

Link Local (RFC 3927)

Private-Use Networks (RFC1918)

IETF Protocol Assignments (RFC5736)

TEST-NET-1 (RFC5737)

6-to-4 Anycast (RFC3068)

Private-Use Networks (RFC1918)

Network Interconnect Device Benchmark Testing (RFC2544)

TEST-NET-2 (RFC5737)

TEST-NET-3 (RFC5737)

Multicast (RFC3171)

Reserved for Future Use (RFC 1112, Section 4)


The default random number generator used in this code is not cryptographically secure. See the rand option to random_ipv4() for information on how to substitute a different random number function.


This version uses a pretty ugly algorithm to generate the IP addresses. It's basically generating a unique IP address and then testing against the exclude list. It'll probably be a lot nicer to call the random function in a way that minimizes the amount of unnecessary calls (I.E. the first call shoudln't generally ask for an integer between zero and 255 since only 1 to 223 is actually allowable). A better approach would be to figure out how many IP addresses are available to be returned and then select a random one of those (basically a pick).

Methods to efficiently select non-duplicate IPs should be available. If the above is done, this should be reasonably feasible.

An OO interface may be nice to minimize per-call processing each time the above are done.

It should be possible to provide ranges that are acceptable to use for the generated IPs. Basically the opposite of "exclude" (but excludes should be applied afterwards still).

IPv6 support must be added. IPv4 is a subset of IPv6, so there should be one set of pick functions and the like, with wrappers to handle conversion of IPv4 to IPv6 and back, when needed.

I have plans to port this to Perl 6.


Joel Maslak <jmaslak@antelope.net>


This software is Copyright (c) 2016 by Joel Maslak.

This is free software, licensed under:

  The Artistic License 2.0 (GPL Compatible)