Sunday Feb 5
May
17/10
Generate Random IP with Python
Last Updated on Saturday, 3 July 2010 03:43
Written by Cody Snider
Monday, 17 May 2010 10:09

In need of an IP address on-the-fly that appears to be valid? Try this:

View Code PYTHON
from random import randrange
 
def generateIP():
    blockOne = randrange(0, 255, 1)
    blockTwo = randrange(0, 255, 1)
    blockThree = randrange(0, 255, 1)
    blockFour = randrange(0, 255, 1)
    print 'Random IP: ' + str(blockOne) + '.' + str(blockTwo) + '.' + str(blockThree) + '.' + str(blockFour)
    if blockOne == 10:
        return self.__generateRandomIP__()
    elif blockOne == 172:
        return self.__generateRandomIP__()
    elif blockOne == 192:
        return self.__generateRandomIP__()
    else:
        return str(blockOne) + '.' + str(blockTwo) + '.' + str(blockThree) + '.' + str(blockFour)

We’re skipping 10.x.x.x, 172.x.x.x and 192.x.x.x due to the fact that these are reserved address. RFC 1918

Version 2:

An elegant solution to serve the purpose of generating a random IP provided by Ben (explanation of changes listed in the comments below):

View Code PYTHON
from random import randrange
if __name__=="__main__":
    not_valid = [10,127,169,172,192]
 
    first = randrange(1,256)
    while first in not_valid:
    first = randrange(1,256)
 
    ip = ".".join([str(first),str(randrange(1,256)),
    str(randrange(1,256)),str(randrange(1,256))])
    print ip

6 Comments
  1. CommentsTweets that mention Generate Random IP with Python | Coding With Cody -- Topsy.com   |  Tuesday, 18 May 2010 at 6:02 pm

    [...] This post was mentioned on Twitter by Cody Snider. Cody Snider said: Generate Random IP with #Python http://goo.gl/fb/M1USV #seo [...]

  2. CommentsWhat is the best way to continually anonymize/randomize my ip address? | Download Zone   |  Saturday, 29 May 2010 at 2:09 am

    [...] Generate Random IP with Python | Coding With Cody [...]

  3. CommentsBen Schmidt   |  Tuesday, 29 June 2010 at 6:56 pm

    Interesting, I appreciate you taking the time to post this. However, there’s a number of issues in your code that I’d like to point out, just for information’s sake. (just suggestions for the future :P )

    A.) The addresses you treat as reserved (anything starting with 0, 192, 172, or 10) don’t actually correspond to the reality of reserved addresses. For one thing, you missed the 127 (loopback) and 169 (link-local) segments. Also, it’s really much more nuanced than cutting whole /8 chunks out of your possible IPs, even according to the RFC, but I realize that it is easier to just do it that way. If you’re interested in reading more: http://bit.ly/ag7a0R (Wikipedia)

    B.) Generating a random value and then, if deemed invalid, recursing, is highly inefficient. It’s much more efficient (no extra stack space needed) to just use a while loop, as well as much cleaner.

    C.) When concatenating strings in Python, it is actually much more efficient to use the join method on a list of strings being concatenated than it is to use the + operator, because Python strings are immutable. If you don’t believe me, here’s a source: http://bit.ly/ag7a0R (wiki.python.org) .

    D.) The range function in Python is non-inclusive of the last number (in your case, 255). This means that in your code, no IPs with the starting octet 255 are generated. Every address starting with 255 except 255.255.255.255 (broadcast) are free to be used, so it hardly seems worth it to wipe out the whole segment just to avoid that.

    E.) A quibble, but you don’t need to specify the step in the range function. It’s automagically 1.

    F.) Another quibble, but you can’t call self.something when you’re not inside an object. I’m sure it was just you pulling it out of previous code though, so it’s all good.

    My implementation would go something like this:

    from random import randrange

    if __name__==”__main__”:
    not_valid = [10,127,169,172,192]

    first = randrange(1,256)
    while first in not_valid:
    first = randrange(1,256)

    ip = “.”.join([str(first),str(randrange(1,256)),
    str(randrange(1,256)),str(randrange(1,256))])
    print ip

    Certainly not perfect, but I think it could be considered an improvement. Anyway, thanks again for taking the time to post this. Keep on hacking

  4. CommentsCody   |  Saturday, 03 July 2010 at 3:30 pm

    Very good points, indeed. Ben’s solution is both elegant and lightweight (I’ll append this code to the main post with all the indentation and characters in order). Thanks for the insight, Ben!

  5. CommentsJ   |  Saturday, 17 September 2011 at 10:31 pm

    You realize that this excludes a large band of IP space 192 that is perfectly valid

  6. CommentsCody Snider   |  Sunday, 18 September 2011 at 1:56 pm

    Indeed, these are valid, but the use case for this would be finding an external IP that’s valid. Using this to find an internal IP would simply require removal of the 192 reserved block exclusion.


Leave a Reply