diff --git a/pedro/ipv6-stuff/overlap.py b/pedro/ipv6-stuff/overlap.py new file mode 100644 index 0000000..4559091 --- /dev/null +++ b/pedro/ipv6-stuff/overlap.py @@ -0,0 +1,53 @@ +import ipaddress +import argparse +# for exit codes +import sys + +def proc_args(): + # src https://docs.python.org/3/library/argparse.html#description + parser = argparse.ArgumentParser(description='Compare two IPs and check if they are overlapped or not') + parser.add_argument('ip1', type=str, + help='IPv6 number 1') + parser.add_argument('ip2', type=str, + help='IPv6 number 2') + args = parser.parse_args() + return args + + # - Write a python script that checks whether two IPv6 networks are overlapping + # - *overlap.py ip1 ip2* + +def parse_ip(ip_arg): + if('/' in ip_arg): + ip_arg = ip_arg.split('/')[0] + else: + # if no network specified, assume a netmask of /48 for all of them + ip_arg = ip_arg + '/48' + # strict false to avoid -> https://stackoverflow.com/questions/40839147/ipaddress-module-valueerrors-has-host-bits-set-self + # strict is passed to IPv4Network or IPv6Network constructor. A ValueError is + # raised if address does not represent a valid IPv4 or IPv6 address, or if + # the network has host bits set. -> src https://docs.python.org/3/library/ipaddress.html#ipaddress.ip_network + return ipaddress.IPv6Network(ip_arg, False) + +def main(): + + is_overlap=False + + args = proc_args() + try: + ip1 = parse_ip(args.ip1) + ip2 = parse_ip(args.ip2) + except: + print('incorrect IPv6 address') + + # src https://docs.python.org/3/library/ipaddress.html#ipaddress.IPv4Network.overlaps + is_overlap = ip1.overlaps(ip2) + + if(is_overlap): + print('overlap') + sys.exit(1) + else: + print('no overlap') + sys.exit(0) + +if __name__ == "__main__": + main() diff --git a/pedro/ipv6-stuff/overlap_run.sh b/pedro/ipv6-stuff/overlap_run.sh new file mode 100755 index 0000000..1e0d0c9 --- /dev/null +++ b/pedro/ipv6-stuff/overlap_run.sh @@ -0,0 +1,32 @@ +#!/bin/sh + +run_program() { + echo "args:\n ip1: ${1}\n ip2: ${2}" + printf 'output: ' + python3 overlap.py ${1} ${2} + echo "exit code: $?" + echo '----------------' +} + +# - Use the following test IPv6 addresses: +# - 2001:db8:: +# - 2001:db8:0:2:: +# - 2001:db8:1:: +# - Step 2: Make your script parse ipv6 networks (like +# 2001:db8::/48 and 2001:db8::/64) ) and check whether they overlap + +ip1='2001:db8::' +ip2='2001:db8:0:2::' +run_program $ip1 $ip2 + +ip1='2001:db8::' +ip2='2001:db8:1::' +run_program $ip1 $ip2 + +ip1='2001:db8:0:2::' +ip2='2001:db8:1::' +run_program $ip1 $ip2 + +ip1='2001:db8::/48' +ip2='2001:db8::/64' +run_program $ip1 $ip2