From c5459e772ac7c4e573f9eb43f58fedb0ff3dd94d Mon Sep 17 00:00:00 2001 From: Luke Tainton Date: Fri, 10 Jul 2020 22:50:25 +0100 Subject: [PATCH 1/3] Make script more Pythonic --- 6to4_converter.py | 65 ++++++++++++++++++++++++++++------------------- 1 file changed, 39 insertions(+), 26 deletions(-) diff --git a/6to4_converter.py b/6to4_converter.py index a7cacf6..9acaa66 100644 --- a/6to4_converter.py +++ b/6to4_converter.py @@ -1,32 +1,45 @@ -ipv4Hex = '' -ipv6Hextet1 = '2002' -ipv6Hextet2 = '' -ipv6Hextet3 = '' +#!/usr/bin/env python3 -#take input -print('Enter IPv4 address:') -ipv4 = input() -#split input to octets -ipv4 = ipv4.split('.',4) +def get_ipv4_from_user(): + user_in = input("Enter IPv4 address: ") + return user_in -#convert IPv4 address to hex string -for octet in ipv4: - if len(hex(int(octet))[2:])>1: - ipv4Hex += hex(int(octet))[2:] - else: - ipv4Hex += '0' - ipv4Hex += hex(int(octet))[2:] -#split into hextets -for i in range(4): - ipv6Hextet2 += (ipv4Hex[i]) -for i in range(4,8): - ipv6Hextet3 += (ipv4Hex[i]) +def ipv4_to_ipv6(ipv4): + ipv4Hex = '' + ipv6Hextet1 = '2002' + ipv6Hextet2 = '' + ipv6Hextet3 = '' -#Convert to dec and back to remove leading zeros -ipv6Hextet2 = hex(int(ipv6Hextet2,16))[2:] -ipv6Hextet3 = hex(int(ipv6Hextet3,16))[2:] + # Split input to octets + ipv4 = ipv4.split('.',4) + + # Convert IPv4 address to hex string + for octet in ipv4: + if len(hex(int(octet))[2:])>1: + ipv4Hex += hex(int(octet))[2:] + else: + ipv4Hex += '0' + ipv4Hex += hex(int(octet))[2:] + + # Split into hextets + for i in range(4): + ipv6Hextet2 += (ipv4Hex[i]) + for i in range(4,8): + ipv6Hextet3 += (ipv4Hex[i]) + + # Convert to dec and back to remove leading zeros + ipv6Hextet2 = hex(int(ipv6Hextet2,16))[2:] + ipv6Hextet3 = hex(int(ipv6Hextet3,16))[2:] + + # Form 6to4 address + output_6to4 = f"{ipv6Hextet1}:{ipv6Hextet2}:{ipv6Hextet3}::/128" + return output_6to4 + + +def main(): + ipv4 = get_ipv4_from_user() + output = ipv4_to_ipv6(ipv4) + print(output) -#form and print 6to4 address -print(ipv6Hextet1+':'+ipv6Hextet2+':'+ipv6Hextet3+'::/128') \ No newline at end of file -- 2.49.1 From c02b08f4d0bf9e5acd6aba85684f1c91298dc70e Mon Sep 17 00:00:00 2001 From: Luke Tainton Date: Fri, 10 Jul 2020 22:53:26 +0100 Subject: [PATCH 2/3] Call main() Helps if you actually call the function doesn't it? --- 6to4_converter.py | 1 + 1 file changed, 1 insertion(+) diff --git a/6to4_converter.py b/6to4_converter.py index 9acaa66..811ac4c 100644 --- a/6to4_converter.py +++ b/6to4_converter.py @@ -43,3 +43,4 @@ def main(): output = ipv4_to_ipv6(ipv4) print(output) +main() -- 2.49.1 From 40b785b6915e8b6c85d93f4fe0d9b3a22695b035 Mon Sep 17 00:00:00 2001 From: Luke Tainton Date: Fri, 19 Feb 2021 20:23:42 +0000 Subject: [PATCH 3/3] Pass IPv4 via CLI argument --- .gitignore | 1 + 6to4_converter.py | 21 ++++++++++++++++----- 2 files changed, 17 insertions(+), 5 deletions(-) create mode 100644 .gitignore mode change 100644 => 100755 6to4_converter.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..723ef36 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.idea \ No newline at end of file diff --git a/6to4_converter.py b/6to4_converter.py old mode 100644 new mode 100755 index 811ac4c..239dd9b --- a/6to4_converter.py +++ b/6to4_converter.py @@ -1,9 +1,18 @@ #!/usr/bin/env python3 +import argparse -def get_ipv4_from_user(): - user_in = input("Enter IPv4 address: ") - return user_in + +def parse_args(): + parser = argparse.ArgumentParser(description='Get 6to4 address from IPv4 address.') + parser.add_argument('-a', '--address', dest='address', action='store', help='IPv4 address', required=True) + args = parser.parse_args() + return args + + +def get_address(): + args = parse_args() + return args.address def ipv4_to_ipv6(ipv4): @@ -39,8 +48,10 @@ def ipv4_to_ipv6(ipv4): def main(): - ipv4 = get_ipv4_from_user() + ipv4 = get_address() output = ipv4_to_ipv6(ipv4) print(output) -main() + +if __name__ == '__main__': + main() -- 2.49.1