Skip to content

Instantly share code, notes, and snippets.

@illuzian
Last active April 4, 2021 23:09
Show Gist options
  • Save illuzian/20ddb25d1745b9b0bc7000bec67e8e8b to your computer and use it in GitHub Desktop.
Save illuzian/20ddb25d1745b9b0bc7000bec67e8e8b to your computer and use it in GitHub Desktop.
Generates a list of IPs with optional address excludes and file output (for autorecon etc)
import ipaddress
import argparse
arg_parser = argparse.ArgumentParser(add_help=True)
arg_parser.add_argument('--subnet','-s', help='The subnet to generate a list for.', required=True, dest='subnet')
arg_parser.add_argument('--exclude', '-e', help='Any IPs that should be excluded from the list. ', nargs='+', dest='exclusions')
arg_parser.add_argument('--out-file', '-o', help='A file to output to', dest='out_file')
args = arg_parser.parse_args()
arg_vars = vars(args)
network = ipaddress.ip_network(arg_vars['subnet'], strict=False)
network_as_list = [str(host) for host in network.hosts()]
if arg_vars['exclusions']:
for ip in arg_vars['exclusions']:
network_as_list.remove(ip)
print(*network_as_list, sep='\n')
formatted = '\n'.join(network_as_list)
if arg_vars['out_file']:
with open(arg_vars['out_file'], 'w') as f:
f.write(formatted)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment