Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Generate Random IP Addresses

by matt.schnarr (Sexton)
on Sep 07, 2004 at 13:25 UTC ( [id://389028]=perlquestion: print w/replies, xml ) Need Help??

matt.schnarr has asked for the wisdom of the Perl Monks concerning the following question:

Hello all,

I was hoping someone might know of a perl module that will allow me to randomly generate an internal or external ip address?

It's not something that would be very hard to do, but I imagine someone's already gone to the trouble of doing this.

Any help would be greatly appreciated!

Thanks!

Matthew Schnarr

Replies are listed 'Best First'.
Re: Generate Random IP Addresses
by hardburn (Abbot) on Sep 07, 2004 at 13:30 UTC

    IP addresses are just 32-bit numbers. If you just need any random address, then:

    my $addr = int rand 2**32; # Perhaps some code for formatting the address into # dotted-quad form

    If you need to make sure the address has certain properties (like "class-B, private address space"), then your task is harder. I'm not sure if there is a module available. It's not that common of a task.

    "There is no shame in being self-taught, only in not trying to learn in the first place." -- Atrus, Myst: The Book of D'ni.

      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@/
        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.
        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: signups1981@yahoo.com
Re: Generate Random IP Addresses
by Anonymous Monk on Sep 07, 2004 at 13:31 UTC
Re: Generate Random IP Addresses
by pelagic (Priest) on Sep 07, 2004 at 13:35 UTC
    #!/usr/bin/perl use strict; srand(time() ^ ($$ + ($$ << 15))); for (1..100){ print join ('.', (int(rand(255)) ,int(rand(255)) ,int(rand(255)) ,int(rand(255)))) , "\n"; } __OUTPUT__ 210.232.115.79 18.239.245.96 15.138.29.10 182.166.124.240 118.118.155.180 80.200.1.109 139.170.113.124 85.13.30.193 80.198.199.175 111.162.166.83 ... a.s.o.

    pelagic
Re: Generate Random IP Addresses
by Pragma (Scribe) on Sep 07, 2004 at 18:29 UTC
    For the obfuscation section:

    s,,...,.s,,int rand 256,eg

      Thank you was useful
      <?php for ($n = 1; $n < 100; $n++) { print implode('.', array(intval(mt_rand(0,255)), intval(mt_rand(0,255)), intval(mt_rand(0,255)), intval(mt_rand(0,255)))) . "\n"; } ?>
Re: Generate Random IP Addresses
by dave_the_m (Monsignor) on Sep 07, 2004 at 13:33 UTC
    Well, it would be absolutely trivial to do, but why would you want a random external IP address?

    Dave.

      ... why would you want a random external IP address?

      I can think of lots of reasons, but none of them are good ones -- like checking for an open mail relay to send your spam through, or directing your script-kiddie exploit tool against...

        The only potentially good reason I can think of is to test your IP-matching regexen. That is, if you're against using Regexp::Common::net.
      Im looking to generate a list of ips starting from something like 71.100.100.180 or something like that. need help doing this. I need to ban a whole city from something lol. :-P I think this is what the monkey guy might be trying to do as well.

        Various ways using various mysteriously named functions from various mysteriously named packages, but it's easy enough with pure perl:

        ## Subs to convert dotted-quads to integers and vice versa sub dq2n{ unpack 'N', pack 'C4', split '\.', $_[ 0 ] };; sub n2dq{ join '.', unpack 'C4', pack 'N', $_[ 0 ] };; $start = dq2n( '71.100.100.180' );; print n2dq( $_ ) for $start .. $start + 100;; 71.100.100.180 71.100.100.181 71.100.100.182 ... 71.100.100.254 71.100.100.255 71.100.101.0 71.100.101.1 71.100.101.2 ... 71.100.101.22 71.100.101.23 71.100.101.24

        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.
      Random sampling in a certain ip range is one reason...

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://389028]
Approved by Arunbear
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (7)
As of 2024-04-18 10:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found