Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??
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@/

In reply to Re^2: Generate Random IP Addresses by fizbin
in thread Generate Random IP Addresses by matt.schnarr

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others romping around the Monastery: (7)
    As of 2014-12-18 01:46 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      Is guessing a good strategy for surviving in the IT business?





      Results (41 votes), past polls