Skip to content

Instantly share code, notes, and snippets.

@marvin
Created March 9, 2022 11:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save marvin/3385a4bea619b14f54fd27e183c51680 to your computer and use it in GitHub Desktop.
Save marvin/3385a4bea619b14f54fd27e183c51680 to your computer and use it in GitHub Desktop.
a script to convert a cidr/ip network list to a list of ip addresses
#!/usr/bin/python3
# writte by David "marvin" Nölte - 2022
# this script reads a file with a list of ip subnets/cirds and outputs it to a ip list
# input file can be a list of subnets/one per line
# check "./cidr2list.py -h"
import os
import ipaddress
import argparse
def main(i,o):
# define variables
iplist = []
networklist = []
# read input file and assign to list
with open(i) as file:
networklist = [line.rstrip() for line in file]
print("Found Networks: " + str(len(networklist)))
# convert every network to ip list and append to iplist
for net in networklist:
for ip in ipaddress.IPv4Network(net):
iplist.append(str(ip))
print("List contains IP Addresses: " + str(len(iplist)))
# write list to file, delete is file exists
if os.path.exists(o):
os.remove(o)
textfile = open(o, "w")
for e in iplist:
textfile.write(e + "\n")
textfile.close()
print("IP List has been written to file: " + o)
if __name__ == "__main__":
parser = argparse.ArgumentParser(description='Optional app description')
parser.add_argument('infile', type=str, help='Input file required')
parser.add_argument('outfile', type=str, help='Output file required')
args = parser.parse_args()
if args.infile and args.outfile:
main(args.infile, args.outfile)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment