53 lines
1.3 KiB
Python
53 lines
1.3 KiB
Python
|
from sys import argv
|
||
|
import argparse
|
||
|
import ipaddress
|
||
|
import random
|
||
|
#import getopt #option function
|
||
|
|
||
|
parser = argparse.ArgumentParser(description="Generating IPv6 networks")
|
||
|
parser.add_argument('--size', help="size")
|
||
|
parser.add_argument('string', nargs='+', help="network")
|
||
|
|
||
|
try:
|
||
|
args = parser.parse_args()
|
||
|
|
||
|
if argv[1] == '--size':
|
||
|
ipnet = argv[3]
|
||
|
size = 128 - int(argv[2])
|
||
|
else:
|
||
|
if len(argv) > 2 :
|
||
|
ipnet = argv[1]
|
||
|
size = 128 - int(argv[2])
|
||
|
else:
|
||
|
ipnet = argv[1]
|
||
|
size = 80
|
||
|
|
||
|
ip = ipnet.split('/')
|
||
|
ip2 = ipaddress.ip_network(ip[0]).supernet(new_prefix=int(ip[1]))
|
||
|
|
||
|
# minimum network
|
||
|
minNet = ipaddress.ip_network(ip[0]).supernet(new_prefix=128-size)
|
||
|
|
||
|
# seperate network
|
||
|
ip2first = ipaddress.IPv6Network(ip2)[0]
|
||
|
ip2last = ipaddress.IPv6Network(ip2)[-1]
|
||
|
minNetlast = ipaddress.IPv6Network(minNet)[-1]
|
||
|
|
||
|
# calculation network
|
||
|
maxRan = int(ip2last) - int(minNetlast) >> size
|
||
|
result = ipaddress.ip_address((random.randrange(0,maxRan) << size) + int(ip2first) )
|
||
|
resultNetwork = ipaddress.ip_network(result).supernet(new_prefix=128-size)
|
||
|
|
||
|
print(resultNetwork)
|
||
|
|
||
|
except:
|
||
|
info="""
|
||
|
wrong input
|
||
|
ex) --size 64 2001:db8::/40
|
||
|
or 2001:db8::/40 64
|
||
|
or 2001:db8::/40 #default size 48
|
||
|
"""
|
||
|
print(info)
|
||
|
|
||
|
|