From 096a2f018a8c278c89c54b4d7db7b83d37492d4b Mon Sep 17 00:00:00 2001 From: Youngjin Han Date: Thu, 30 Jul 2020 00:50:15 +0900 Subject: [PATCH] Learning Circle : IPV6 #2 --- youngjin.han/ipv6/ipv6.org | 3 ++ youngjin.han/ipv6/source/randomnet.py | 59 +++++++++++++++++++++++++++ youngjin.han/learning-node02-2020.org | 21 ++++++++++ 3 files changed, 83 insertions(+) create mode 100644 youngjin.han/ipv6/source/randomnet.py diff --git a/youngjin.han/ipv6/ipv6.org b/youngjin.han/ipv6/ipv6.org index 982f221..c72bae3 100644 --- a/youngjin.han/ipv6/ipv6.org +++ b/youngjin.han/ipv6/ipv6.org @@ -1,3 +1,6 @@ +* 2020-07-24 +** note + - none * 2020-07-22 ** note - none diff --git a/youngjin.han/ipv6/source/randomnet.py b/youngjin.han/ipv6/source/randomnet.py new file mode 100644 index 0000000..2cc835e --- /dev/null +++ b/youngjin.han/ipv6/source/randomnet.py @@ -0,0 +1,59 @@ +# from sys import argv +import argparse +import ipaddress +import random + +# script, prefix = argv + +def main(ipv6net_list, size_list): + """ + 메인 함수 + """ + if 2 == len(ipv6net_list): + ipv6net_src = ipaddress.IPv6Interface(ipv6net_list[0]) + size = ipv6net_list[1] + print('Input : {}'.format(ipv6net_src)) + print('Size : {}'.format(size)) + elif 2 > len(ipv6net_list): + ipv6net_src = ipaddress.IPv6Interface(ipv6net_list[0]) + size = size_list + print('Input : {}'.format(ipv6net_list)) + print('Size : {}'.format(size)) + else: + ipv6net_src = [] + size = size_list + print('Please, Read Help') + + ipv6_rand = 0 + size_rand = int(size)-int(str(ipv6net_src)[str(ipv6net_src).find('/')+1:]) + while size_rand != 0: + if 16 <= size_rand: + ipv6_rand = ipv6_rand << 16 + ipv6_rand += random.randint(0, (2**16)-1) + size_rand -= 16 + else: + ipv6_rand = ipv6_rand << size_rand + ipv6_rand += random.randint(0, (2**size_rand)-1) + size_rand = 0 + +# print('{}'.format(ipaddress.IPv6Network((ipv6net_src)))) +# print('{}'.format(str(ipaddress.IPv6Address((ipv6_rand<<(128-int(size)))))+'/'+size)) +# print('{}'.format(int(size)-int(str(ipv6net_src)[str(ipv6net_src).find('/')+1:]))) + print('Output : {}'.format(str(ipaddress.IPv6Address(int(ipv6net_src)+(ipv6_rand <<(128 - int(size)))))+'/'+size)) + +def get_arguments(): + """ + argument를 파싱해서 가져옴 + """ + parser = argparse.ArgumentParser() + parser.add_argument('ipv6net', nargs='+', help='Example) 2001:db8::/40 or 2001:db8::/40 64', type=str) + parser.add_argument('--size', nargs='?', help='Example) 64...0', default='48', type=str) + + ipv6net_list = parser.parse_args().ipv6net + size_list = parser.parse_args().size + + return ipv6net_list, size_list + +if __name__ == '__main__': + ipv6net_list, size_list = get_arguments() + main(ipv6net_list, size_list) diff --git a/youngjin.han/learning-node02-2020.org b/youngjin.han/learning-node02-2020.org index 0e90c5f..69a7f6f 100644 --- a/youngjin.han/learning-node02-2020.org +++ b/youngjin.han/learning-node02-2020.org @@ -1,3 +1,24 @@ +* 2020-07-24 +*** IPv6 #2: Generating IPv6 networks +**** DONE Lecture content + CLOSED: [2020-07-30 목 00:43] + - Objective/Problem that we are trying to solve: + - You got one bigger prefix (the one that you get as a + parameter) and you want to create a random subnetwork in it + - Create a random /48 network within a prefix + - Create a python script named *randomnet.py* that accepts 1 + parameter: *prefix* + - The prefix must be an IPv6 network + - The netmask of the prefix should be /48 or smaller (/48.../0) + - Prefixes to be used for testing: + - 2001:db8::/48 + - 2001:db8::/40 + - 2001:db8::/32 + - fd00::/8 + - Possible output: + - 2001:db8:1231::/48 + - Optional + - Allow to specify second parameter that gives the size of the subnetwork * 2020-07-22 *** IPv6 #1: IPv6 address uniqueness **** DONE Lecture content