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.