Skip to content

Instantly share code, notes, and snippets.

@fjrti
Created August 30, 2020 08:34
Show Gist options
  • Save fjrti/f9bc8fcf728d92c43ea04498608e4059 to your computer and use it in GitHub Desktop.
Save fjrti/f9bc8fcf728d92c43ea04498608e4059 to your computer and use it in GitHub Desktop.
cidr list
# -*- coding: utf-8 -*-
from netaddr import IPNetwork
import click
@click.command()
@click.argument('cidr')
@click.option('--seperator', '-s', default=',', help='List seperator.')
@click.option('--file', '-f', default=False, help='txt file output path.')
@click.option('--range/--no-range', default=False, help='Output first and last IP Addresses')
def main(cidr, seperator, file, range):
"""
Convert CIDR to list of IPs
"""
ip = IPNetwork(cidr)
string = seperator.strip().join([str(i) for i in ip])
if range:
string = '{}-{}'.format(ip[0], ip[-1])
if file:
with open(file, 'w') as f:
f.write(string)
else:
click.echo(string)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment