0

I have a file with CIDRs inside like this:

1.2.3.4/10
5.6.7.8/20
...

I want to read the CIDR file and then write all IPs to another file.

Already tried this python code but dont know how to read each line and then do this:

from netaddr import *

f = open("everyip.txt", "w")
ip = IPNetwork('10.0.0.0/8')

for addr in ip:
    f.write(str(addr) + '\n')

f.close()

Edit

I already tried this as well:

from netaddr import *

f = open("everyip.txt", "w")

with open("cidrs.txt") as f:
  content = f.readlines()
  content = [x.strip() for x in content] 
  str1 = ''.join(content)
  ip = IPNetwork(str1)

for addr in ip:
  f.write(str(addr) + '\n')

f.close()

This works only for 1 line in the cidrs.txt. If there is more than 1 line in cidrs.txt, it throws an error and doesnt work.

Edit 2:

I tried this but get an error:

from netaddr import *
i = 0
f = open("everyip.txt", "w")
with open("range.txt") as f1:
    content = f1.readlines()
    content = [x.strip() for x in content]
    while (i < len(content)):
        ip = IPNetwork(content[i])
        for addr in ip:
            f.write(str(addr) + '\n')
        f.close()

Error:

f.write(str(addr) + '\n')
ValueError: I/O operation on closed file.
tripleee
  • 175,061
  • 34
  • 275
  • 318
  • @TheGeneral here you go! :| – aria darkkkis Jan 15 '19 at 03:42
  • From one of the related question, if you want to read a file line by line: [How to read a file line-by-line into a list](https://stackoverflow.com/questions/3277503/how-to-read-a-file-line-by-line-into-a-list?rq=1). – metatoaster Jan 15 '19 at 03:44
  • @metatoaster I exactly tried this aswell and converted the line to string using `''.join`. But the problem is I cant make it to read 1 line , extract IP , go to another line. Same loop until end of file. – aria darkkkis Jan 15 '19 at 03:46
  • Based on what you have, `f.readlines()` already give you a list, just iterate through each of those items, not sure why you would `join` all the content into `str1` which gives you a single item, which gave you the effect that you can't read a line, when you already did. What you should do is create an `IPNetwork` object for every item inside `content`, and work with that object to create whatever you need to do. – metatoaster Jan 15 '19 at 03:52
  • @metatoaster yea you are right. No need to convert to string. But I cant get it in a loop. Edited the code. – aria darkkkis Jan 15 '19 at 04:09
  • You closed the file inside the loop. Why didn't you use the `with` statement when opening `everyip.txt` like you did for `range.txt`? Also instead of `while i < len(...)` to iterate through the list you should use a for loop, much like what you did for `for addr in ip`. – metatoaster Jan 15 '19 at 04:15
  • @metatoaster Do you know how can I parse the CIDRs before running app? For example, I have a CIDRs in a file like this `1.2.3.4/15 , 2.2.2.2/10 , 2.3.4.5/12`. So open the `cidr.txt` and then parse CIDRs in each line and then go on with the rest of the program/ – aria darkkkis Jan 15 '19 at 04:31
  • Please try searching for something like "python parsing comma separated file" on StackOverflow and/or your favorite search engine and see how it might work. Programming involves actual problem solving, failing that it requires actually looking up help files. Failing that, there are many free resources and tutorials that will aid you in getting started with writing a program (in any programming language(s) you feel comfortable with). – metatoaster Jan 15 '19 at 04:37
  • @metatoaster EDIT 3, only runs 1 time. Guess it always sets i to 0 and then exit program. it doesnt get in a loop – aria darkkkis Jan 15 '19 at 04:37
  • @metatoaster fixed. thanks for help. – aria darkkkis Jan 15 '19 at 04:44
  • I rolled back your latest edits. Your question should remain strictly a question. You are more than welcome to post an answer with the fixed solution; your edits are visible in the [edit history](https://stackoverflow.com/posts/54192380/revisions). – tripleee Jan 15 '19 at 04:53

1 Answers1

1

The OP posted this as an edit to their question.

Fixed:

from netaddr import *
with open("range.txt") as f1:
    content = f1.readlines()
    content = [x.strip() for x in content]
    with open("everyip.txt", "w") as f:
        for i in content:
            ip = IPNetwork(i)
            for addr in ip:
                f.write(str(addr) + '\n')

If there is a large number of lines, it is better to process them one at a time instead of reading them all into memory.

from netaddr import *
with open("range.txt") as f1, open("everyip.txt", "w") as f:
    for line in f1:
        cidr = line.rstrip('\n')
        ip = IPNetwork(cidr)
        for addr in ip:
            f.write(str(addr) + '\n')
tripleee
  • 175,061
  • 34
  • 275
  • 318
  • If you'd rather post your own answer and accept that, ping me and we can get this removed. – tripleee Jan 15 '19 at 05:01
  • Welp, don't close a file whilst you are in the middle of writing to it. I don't think there is anything here you haven't already seen in your own code. – tripleee Jan 15 '19 at 05:08
  • yea just wanted to know what's going on that is different than my code. Thanks – aria darkkkis Jan 15 '19 at 05:22