Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical

Re^2: Generate Random IP Addresses

by fizbin (Chaplain)
on Sep 07, 2004 at 14:03 UTC ( #389049=note: print w/replies, xml ) Need Help??

in reply to Re: Generate Random IP Addresses
in thread Generate Random IP Addresses

If you need to make sure the address has certain properties (like "class-B, private address space"), then your task is harder.
But not really by much. Any common address space allocation can be described by a netmask and a sample address. So, then, you just do this:
# for 192.168.*.* my @netmask_a = qw ( 255 255 0 0 ); my @sample_a = qw ( 192 168 0 0 ); my $netmask = unpack("N", pack("C*", @netmask_a)); my $sample = unpack("N", pack("C*", @sample_a)); my $addr = int rand(2**32); $addr &= ~ $netmask; $addr ^= $sample; # Dotted quad form: my $dottedaddr = join(".", unpack("C*", pack("N", $addr)));
If you wanted to be a bit more efficient, you might adjust the "32" in the rand line based on how many bits of randomness you actually need, but I'm not actually sure that buys you anything, and it hurts maintainability when you forget to switch it back when you're looking through a larger address range. If you're really hurting for efficiency, and have this in a tight loop, compute $netmask and $sample from the arrays outside the tight loop, and pass both in, as in:
sub rand_addr ($$) { my ($netmask, $code) = @_; my $addr = int rand(2**32); $addr &= ~ $netmask; $addr ^= $sample; $addr; } # for 192.168.*.* my @netmask_a = qw ( 255 255 0 0 ); my @sample_a = qw ( 192 168 0 0 ); my $netmask = unpack("N", pack("C*", @netmask_a)); my $sample = unpack("N", pack("C*", @sample_a)); my @newaddrlist = map { join(".", unpack("C*", pack("N",$_))) } map { rand_addr($netmask,$sample); } ( 1 .. 5678 );
but for anything other than intrusion test systems, I can't imagine why efficiency would be important.

Note that none of this code has been tested, but it all looks vaguely right.

-- @/=map{[/./g]}qw/.h_nJ Xapou cets krht ele_ r_ra/; map{y/X_/\n /;print}map{pop@$_}@/for@/

Replies are listed 'Best First'.
Re^3: Generate Random IP Addresses
by Anonymous Monk on Oct 26, 2004 at 02:50 UTC
    For those stumbling across this post in the future, I wanted to add a comment that I don't believe generating a random 32-bit number is the proper way to generate a random ip address. As illustrated in some of the other posts, you need to generate a random number for each octect. This is not blatantly obvious and was a minor stumbling block while trying to figure out how to use that code.
Re^3: Generate Random IP Addresses
by Anonymous Monk on Jul 16, 2008 at 21:13 UTC
    OK wait, I m a just a beginner at this, so i have one question: WHERE DO YOU TYPE ALL OF THIS ?! please send me an email as-soon-as possible, I'll really appreciate it, Thanks. email:

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://389049]
[Corion]: Heh. Perl heredocs are nasty - Filter::Simple doesn't know whether the line continued after the heredoc or not. The heredoc payload starts on the next line.
[Corion]: So currently, Filter::Simple mangles your code/line numbers if you use heredocs ;)

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (6)
As of 2017-01-22 10:17 GMT
Find Nodes?
    Voting Booth?
    Do you watch meteor showers?

    Results (187 votes). Check out past polls.