Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
cidr-to-iplist.py
#!/usr/bin/env python
from netaddr import IPNetwork
import sys
from random import shuffle
if len(sys.argv) != 3:
sys.exit("Input and output file is needed")
file_with_cidr_data = sys.argv[1]
file_to_save_ip_list_to = sys.argv[2]
try:
lines = open(file_with_cidr_data,"r").readlines()
except Exception as e:
sys.exit("Input '{}' file does not exist!".format(file_with_cidr_data))
pass
ips_list = []
for line in lines:
if line.strip() == "":
continue
for ip in IPNetwork(line):
print("{}".format(ip))
ips_list.append("{}".format(ip))
shuffle(ips_list)
ips = "\n".join(ips_list)
f = open(file_to_save_ip_list_to, "w")
f.write(ips)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.