ungleich-learning-circle/kjg/IPv6/ula/ularegistry/randomcreate.py

29 lines
777 B
Python

from sys import argv
import argparse
import ipaddress
import random
def createIP(ip):
ipnet = ip
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)
return resultNetwork