0

I'm pretty new to python and this might be an easy anwser but How can i generate random ips from 0.0.0.0/0 using ipaddress module.

main.py

import ipaddress

def ipv4():
    net4 = ipaddress.ip_network('0.0.0.0/0')
    for ips in net4:

ipv4()

Current output

0.0.149.149
0.0.149.150
0.0.149.151
0.0.149.152

Expected output

193.189.190.7
154.195.199.61
199.231.163.232
179.112.190.133
188.120.233.146
  • What do you mean by random IPs? Can you provide the expected output? – Robert Seaman Aug 22 '20 at 09:51
  • Not sure if the `ipaddress` module can be used for this (why would you need it?). Have you tried just using `random.randint` instead? – tobias_k Aug 22 '20 at 09:53
  • 1
    Just remember, an IP address is just a 32bit integer (4x 8bits). That means you can randomise a number between `0 -> 2,147,483,647` and use examples from this post to convert that integer into a IP string: https://stackoverflow.com/a/22272197/6281755 – Robert Seaman Aug 22 '20 at 09:55
  • i tried netaddr, random but they don't work for my specific case –  Aug 22 '20 at 09:56
  • 2
    Do you have additional requirements like: "the addresses need to be non-private, publicly routable" or similar? Or do you just need some things that look like IP addresses? – kerasbaz Aug 22 '20 at 10:01
  • i need random publicly routable ips –  Aug 22 '20 at 10:03
  • Can you please clearly define what you need? In a comment you say "all the random ips in 0.0.0.0/0" – there are no random IPs in 0.0.0.0/0, and randomly picking defeats having all. Do you want all IPv4 addresses in random order? – MisterMiyagi Aug 22 '20 at 11:51

4 Answers4

2

how about the below?

import ipaddress
import random
import sys

print(str(ipaddress.IPv4Address(random.randint(0,2 ** 32))))
balderman
  • 22,927
  • 7
  • 34
  • 52
0

One possible solution:

import random

MAX_IPV4 = ipaddress.IPv4Address._ALL_ONES  # 2 ** 32 - 1
MAX_IPV6 = ipaddress.IPv6Address._ALL_ONES  # 2 ** 128 - 1


def random_ipv4():
    return  ipaddress.IPv4Address._string_from_ip_int(
        random.randint(0, MAX_IPV4)
    )

def random_ipv6():
    return ipaddress.IPv6Address._string_from_ip_int(
        random.randint(0, MAX_IPV6)
    )

Examples:

    >>> random.seed(444)                                                                                                                                                                                                                                         
    >>> random_ipv4()                                                                                                                                                                                                                                            
    '79.19.184.109'
    >>> random_ipv4()                                                                                                                                                                                                                                            
    '3.99.136.189'
    >>> random_ipv4()                                                                                                                                                                                                                                            
    '124.4.25.53'
    >>> random_ipv6()                                                                                                                                                                                                                                            
    '4fb7:270d:8ba9:c1ed:7124:317:e6be:81f2'
    >>> random_ipv6()                                                                                                                                                                                                                                            
    'fe02:b348:9465:dc65:6998:6627:1300:29c9'
    >>> random_ipv6()                                                                                                                                                                                                                                            
    '74a:dd88:1ff2:bfe3:1f3:81ad:debd:db88'

If ipaddress is optional, you can use this

>>> import random
>>> import socket
>>> import struct
>>> socket.inet_ntoa(struct.pack('>I', random.randint(1, 0xffffffff)))
'197.38.59.143'
>>> socket.inet_ntoa(struct.pack('>I', random.randint(1, 0xffffffff)))
'228.237.175.64'

If you want multiple IPs, put it in a loop like this,

for _ in range(10):
    print(socket.inet_ntoa(struct.pack('>I', random.randint(1, 0xffffffff))))

Output:

151.166.184.41
237.59.227.33
249.179.144.26
248.151.116.150
94.186.44.162
251.238.134.106
5.239.51.182
198.1.81.192
24.116.237.239
187.8.43.55
surya
  • 719
  • 5
  • 13
0

This code is for Class C IP addresses:

#Class C IP address range 192.168.0.0 - 192.168.255.255
import ipaddress
import random
def ipv4(no_ips):
    for i in range(1,no_ips):        
        netwrk_id = random.choice(range(0,255))
        host_id = random.choice(range(0,255))
        ip_address_gen = f'192.i68.{netwrk_id}.{host_id}'
        print(ip_address_gen)
ipv4(20) #this generates 20 Class C IP addresses

Here i have imported the random module and i'm using it to generate random integers between 0 and the Network ID/Host ID limit(i.e 255). And then add 192.168(as is the case for Class C addresses) to the generated values. While calling the ipv4 function, you should just pass the number of IP addresses you want to generate as and argument. You may extend this code for Class B IP addresses, in which case you'll be generating three different set of integers since Class B range looks like this: 172.16.0.0 - 172.31.255.255

Seyi Daniel
  • 2,259
  • 2
  • 8
  • 18
0

This is a slightly improved version of the code suggested by @balderman.

Generate a valid random IP Address:

import ipaddress 
import random

def get_random_ip():
    """Generate a random IP Address
    Returns:
        string: IP Address
    """
    randomIPAddress = str(ipaddress.IPv4Address(random.randint(0, 2 ** 32)))
    isPrivateIp = ipaddress.ip_address(randomIPAddress).is_private
    if isPrivateIp:
        return get_random_ip()
    return randomIPAddress
user3785966
  • 2,578
  • 26
  • 18