Help language development. Donate to The Perl Foundation

IP::Random cpan:JMASLAK last updated on 2018-07-01


Build Status


IP::Random - Generate random IP Addresses


use IP::Random;

my $ipv4 = IP::Random::random_ipv4;
my @ips  = IP::Random::random_ipv4( count => 100 );


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 the default exclude list for IPv4, as a list of CIDR strings.

Additional CIDRs may be added to future versions, but in no case will standard Unicast publicly routable IPs be added. See named_exclude to determine what IP ranges will be included in this list.


When passed a $type, such as 'rfc1918', will return a list of CIDRs that match that type. See named_exclude for the valid types.

random_ipv4( :@exclude, :$count )

say random_ipv4;
say random_ipv4( exclude => ('rfc1112', 'rfc1122') );
say random_ipv4( exclude => ('default', '') );
say join( ',',
    random_ipv4( exclude => ('rfc1112', 'rfc1122'), count => 2048 ) );

This returns a random IPv4 address. If called with no parameters, it will exclude any addresses in the default exclude list.

If called with the exclude optional parameter, which is passed as a list, it will use the exclude types (see named_exclude for the types) to exclude from generation. In addition, individual CIDRs may also be passed to exclude those CIDRs. If neither CIDRs or exclude types are passed, it will use the default tag to exclude the default excludes. Should you want to exclude nothing, pass an empty list. If you want to exclude something in addition to the default list, you must pass the default tag explictly.

The count optional parameter will cause c to return a list of random IPv4 addresses (equal to the value of count). If count is greater than 128, this will be done across multiple CPU cores. Batching in this way will yield significantly higher performance than repeated calls to the random_ipv4() routine.



%excludes = IP::RANDOM::named_exclude

A hash of all the named IP exludes that this IP::Random is aware of. The key is the excluded IP address. The value is a list of tags that this module is aware of. For instance, would be a key with the values of ( 'rfc1918', 'default' ).

This list contains:


Joelle Maslak


Elizabeth Mattijsen


Copyright (C) 2018 Joelle Maslak

This library is free software; you can redistribute it and/or modify it under the Artistic License 2.0.