Make script more Pythonic
This commit is contained in:
@@ -1,16 +1,21 @@
|
||||
#!/usr/bin/env python3
|
||||
|
||||
|
||||
def get_ipv4_from_user():
|
||||
user_in = input("Enter IPv4 address: ")
|
||||
return user_in
|
||||
|
||||
|
||||
def ipv4_to_ipv6(ipv4):
|
||||
ipv4Hex = ''
|
||||
ipv6Hextet1 = '2002'
|
||||
ipv6Hextet2 = ''
|
||||
ipv6Hextet3 = ''
|
||||
|
||||
#take input
|
||||
print('Enter IPv4 address:')
|
||||
ipv4 = input()
|
||||
|
||||
#split input to octets
|
||||
# Split input to octets
|
||||
ipv4 = ipv4.split('.',4)
|
||||
|
||||
#convert IPv4 address to hex string
|
||||
# Convert IPv4 address to hex string
|
||||
for octet in ipv4:
|
||||
if len(hex(int(octet))[2:])>1:
|
||||
ipv4Hex += hex(int(octet))[2:]
|
||||
@@ -18,7 +23,7 @@ for octet in ipv4:
|
||||
ipv4Hex += '0'
|
||||
ipv4Hex += hex(int(octet))[2:]
|
||||
|
||||
#split into hextets
|
||||
# Split into hextets
|
||||
for i in range(4):
|
||||
ipv6Hextet2 += (ipv4Hex[i])
|
||||
for i in range(4,8):
|
||||
@@ -28,5 +33,13 @@ for i in range(4,8):
|
||||
ipv6Hextet2 = hex(int(ipv6Hextet2,16))[2:]
|
||||
ipv6Hextet3 = hex(int(ipv6Hextet3,16))[2:]
|
||||
|
||||
#form and print 6to4 address
|
||||
print(ipv6Hextet1+':'+ipv6Hextet2+':'+ipv6Hextet3+'::/128')
|
||||
# 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)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user