Add simple mac2eui64 utility

Add a small utility to generate EUI-64 addresses from MAC-48 + IPv6 prefix.
Signed-off-by: default avatarApollon Oikonomopoulos <apollon@noc.grnet.gr>
parent a2eba3d0
#!/usr/bin/env python
import sys
from IPy import IP
if len(sys.argv) != 3:
sys.stderr.write("Usage: %s <mac_address> <IPv6 prefix>\n" % sys.argv[0])
sys.exit(127)
mac = sys.argv[1]
try:
prefix = IP(sys.argv[2])
except ValueError:
sys.stderr.write("Invalid IPv6 prefix '%s'\n" % sys.argv[2])
if prefix.version() != 6:
sys.stderr.write("%s is not a valid IPv6 prefix\n" % prefix)
sys.exit(1)
if prefix.prefixlen() != 64:
sys.stderr.write("Cannot generate an EUI-64 address on a non-64 subnet\n")
sys.exit(1)
mac_parts = mac.split(":")
pfx_parts = prefix.net().strFullsize().split(":")
if len(mac_parts) != 6:
sys.stderr.write("%s is not a valid MAC-48 address\n" % mac)
sys.exit(1)
eui64 = mac_parts[:3] + [ "ff", "fe" ] + mac_parts[3:]
eui64[0] = "%02x" % (int(eui64[0], 16) ^ 0x02)
ip = ":".join(pfx_parts[:4])
for l in range(0, len(eui64), 2):
ip += ":%s" % "".join(eui64[l:l+2])
try:
print IP(ip).strCompressed()
except ValueError, e:
sys.stderr.write("Ooops, something went wrong: '%s'!\n" % str(e))
# vim: set ts=4 sts=4 sw=4 et ai :
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment