1
$\begingroup$

I know this was asked before, but I didn't get a definitive answer. I am trying to download a simple 17GB .txt file that contains the entire IPv4 address space. There must be a file like this already on the web.

$\endgroup$
1
$\begingroup$

You can simply generate them yourself. They are simply representations of all integers up to $2^{32}$

Which format do you want the address? There are many ways to represent them according to your requirements, due to the fact that they are simply representation of a set of normal numbers:

  • standard dot-separated e.g. 192.168.0.1
  • unsigned int32 : e.g 3548551264
  • hex, dotted hex, octal bytes

You could generate them in Python like this:

def generate_ips(): 
    """Generate all possible IPv4 addresses, on by one""" 
    for i in range(255): 
        for j in range(255):
            for k in range(255):
                for l in range(255):
                    yield "{0}.{1}.{2}.{3}".format(i, j, k, l)

The you must loop on the generator function like this to actuall use the values:

for ip in generate_ips(): 
    print(ip) 

# output
0.0.0.0
0.0.0.1
0.0.0.2
0.0.0.3
0.0.0.4
0.0.0.5
0.0.0.6
0.0.0.7

I used a generator (with the yield keyword instead of saving all values in a list) because it will just give you one at a time and not consume all those Gb of memory that you mentioned!

If you want to store all values in a list for some reason, you can alter the function to append each IP address to a list, then return that:

def generate_ips():
    """Return a list of all possible IPv4 addresses"""
    all_ips = []
    for i in range(255):
        for j in range(255):
            for k in range(255):
                for l in range(255):
                    ip = "{0}.{1}.{2}.{3}".format(i, j, k, l)
                    all_ips.append(ip)
    return all_ips
$\endgroup$
  • $\begingroup$ Could we move this to chat for you to show me out to store them all in a list? $\endgroup$ – C. Rutscher May 28 '19 at 16:09
  • $\begingroup$ Are these two separate python files? $\endgroup$ – C. Rutscher May 28 '19 at 16:09
  • $\begingroup$ @C.Rutscher - I have added a simple list version. You can put the functions in the same file (give them different function names after def) and just run whichever one you need. $\endgroup$ – n1k31t4 May 28 '19 at 16:12
  • $\begingroup$ How do I output it to a .txt file though? $\endgroup$ – C. Rutscher May 28 '19 at 16:17
  • $\begingroup$ @C.Rutscher - Have alook for a tutorial (like this one) online somewhere. There are lots! $\endgroup$ – n1k31t4 May 28 '19 at 17:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.