Skip to content

Instantly share code, notes, and snippets.

@zed
Last active December 27, 2015 07:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zed/89ece9515a748afd6798 to your computer and use it in GitHub Desktop.
Save zed/89ece9515a748afd6798 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
"""Convert ip address to RFC3056 IPv6 6to4 address."""
import binascii
import ipaddress
ip4str = input("Enter IPv4 (e.g., 9.254.253.252):")
ip4 = ipaddress.IPv4Address(ip4str)
h = binascii.hexlify(ip4.packed).decode()
ip6 = ipaddress.IPv6Address("2002:" + h[:4] + ":" + h[4:] + "::")
print()
print("ip4 address: ", ip4)
print("Integer (decimal): ", int(ip4))
print("Integer (binary): ", bin(int(ip4))[2:])
print("Ingeger (hexadecimal): ", h)
assert len(h) == 8
print("6to4 address: ", ip6)
assert ip6.sixtofour == ip4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment