Created
August 30, 2020 08:34
-
-
Save fjrti/f9bc8fcf728d92c43ea04498608e4059 to your computer and use it in GitHub Desktop.
cidr list
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- 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