Skip to content

Instantly share code, notes, and snippets.

@bp-
Created September 11, 2013 08:59
Show Gist options
  • Save bp-/6521070 to your computer and use it in GitHub Desktop.
Save bp-/6521070 to your computer and use it in GitHub Desktop.
Converts a mac address to the corresponding EUI 64 IPv6.
from netaddr import *
def mac2ipv6( mac, prefix ):
mac = EUI(mac).eui64()
bits = list(str(mac.bits()))
bits[6] = "1"
bits = "".join(bits)
bytes = bits.split("-");
bytes = [''.join([bytes[0], bytes[1]]), ''.join([bytes[2], bytes[3]]), ''.join([bytes[4], bytes[5]]), ''.join([bytes[6], bytes[7]]) ]
ipv6 = [prefix];
for byte in bytes:
ipv6.append(hex(int(byte, 2))[2:6])
return ":".join(ipv6)
prefix = "2001:4001:dead:beef"
mac = "da:a0:b1:c2:d3:e4"
print(mac2ipv6(mac, prefix))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment