Skip to content

Instantly share code, notes, and snippets.

@Damian89
Created August 14, 2019 08:09
Show Gist options
  • Save Damian89/57a43c7aae792baffe29e93f23c31e17 to your computer and use it in GitHub Desktop.
Save Damian89/57a43c7aae792baffe29e93f23c31e17 to your computer and use it in GitHub Desktop.
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