Last active
August 29, 2015 14:14
-
-
Save herrfz/b4527e76cc72a4c3c352 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Python 3 | |
def reverse_dotted_ipv6(address): | |
ADDR_HEX_LEN = 32 | |
GROUP_LEN = 4 | |
try: | |
splitted = address.replace('::', ':0000:').split(':') # check for grouping format | |
if len([x for x in splitted if len(x) != GROUP_LEN]) != 0: | |
return '(╯°□°)╯︵ ┻━┻)' | |
bytearray.fromhex(address.replace(':', '')) # check for non-hex character | |
except (AttributeError, ValueError): | |
return '(╯°□°)╯︵ ┻━┻)' | |
nhex = len([x for x in address if x != ':']) | |
nmissing = ADDR_HEX_LEN - nhex | |
stripped_addr = address.replace('::', nmissing * '0').replace(':', '') | |
if len(stripped_addr) != ADDR_HEX_LEN: # check for correct address length | |
return '(╯°□°)╯︵ ┻━┻)' | |
return '.'.join(reversed(stripped_addr)) | |
print(reverse_dotted_ipv6('FE80::0202:B3FF:FE1E:8329')) # 9.2.3.8.E.1.E.F.F.F.3.B.2.0.2.0.0.0.0.0.0.0.0.0.0.0.0.0.0.8.E.F | |
print(reverse_dotted_ipv6('FE80::0202:B3FF:FE1E:8329:DEAD:BEEF:DEAD:CAFE')) # (╯°□°)╯︵ ┻━┻) | |
print(reverse_dotted_ipv6('FE80::0202:B3FF:FE1E:8329:xxxx')) # (╯°□°)╯︵ ┻━┻) | |
print(reverse_dotted_ipv6('FE80:0202:B3FF:FE1E:8329')) # (╯°□°)╯︵ ┻━┻) | |
print(reverse_dotted_ipv6('192::168:2:1')) # (╯°□°)╯︵ ┻━┻) | |
print(reverse_dotted_ipv6(123)) # (╯°□°)╯︵ ┻━┻) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment