55 lines
1.5 KiB
Python
55 lines
1.5 KiB
Python
import ipaddress
|
|
import argparse
|
|
# for exit codes
|
|
import sys
|
|
|
|
# this is useful for debugging (put it in the line you want to inspect)
|
|
# import IPython; IPython.embed()
|
|
|
|
def proc_args():
|
|
# src https://docs.python.org/3/library/argparse.html#description
|
|
parser = argparse.ArgumentParser(description='Generate a random IPv6 network that is subnetwork from the the input network. Optional parameter allows you to specify the size of the subnetwork')
|
|
parser.add_argument('prefix', type=str,
|
|
help='IPv6 network')
|
|
parser.add_argument('--size', type=int,
|
|
help='prefix of the output IPv6 subnetwork')
|
|
args = parser.parse_args()
|
|
return args
|
|
|
|
def proc_input(args):
|
|
prefix = args.prefix
|
|
try:
|
|
net6 = ipaddress.IPv6Network(prefix)
|
|
except:
|
|
print('input is not IPv6 network or is not in CIDR format')
|
|
sys.exit(1)
|
|
if(net6.prefixlen > 48):
|
|
print('The netmask of the prefix should be /48 or smaller (/48.../0)')
|
|
sys.exit(1)
|
|
if args.size:
|
|
if(args.size < 48):
|
|
print('Prefix subnet size should be greater or equal than 48')
|
|
sys.exit(1)
|
|
size = args.size
|
|
else:
|
|
size = 48
|
|
if net6.prefixlen = size:
|
|
print('the input subnet cannot be equal to the output subnet')
|
|
sys.exit(1)
|
|
return net6, size
|
|
|
|
def main():
|
|
|
|
args = proc_args()
|
|
|
|
net6, size = proc_input(args)
|
|
import IPython; IPython.embed()
|
|
|
|
# generate random subnet
|
|
net6.subnets(new_prefix=size)
|
|
|
|
print(subprefix)
|
|
|
|
if __name__ == "__main__":
|
|
main()
|