Network MAC address conversion to IPv6 link-local address One of the ways I learn a new scripting language is to implement a subroutine to convert a network MAC addr into the IPv6 link-local address, as described in RFC 4862. So here is a quick few lines. 

My question to you is:


can this code be shrunk any more?
is there a more pythonic way of doing this?
is there anything non-optimal? 
is the code future proof to 3.4 python?


Especially with casting a string to an int, and finally back to hex, seems overly weird.

Also, I did recently discover some IP related factory functions, but so far I've not discovered any builtin way of doing this. I'm using python 2.7, but my Linux distro is soon to switch to 3.x.

#!/usr/bin/python
"""
convert mac addr to ipv6 link local (rfc 4862)
"""

# the mac should become: fe80::2177:02ff:fed2:ff9b
mac='23:77:02:d2:ff:9b'

def mac_to_ipv6_linklocal(mac):

  # remove the most common macaddr delimiters, dots, dashes, etc.
  # cast the hex string to an base-16 int for math safety.
  # use xor of 02 on the 2nd most significant hex char.
  # Remove/slice the '0x' off the begining from using hex().

  m = hex(int(mac.translate(None,' .:-'),16)^0x020000000000)[2:]
  return 'fe80::%s:%sff:fe%s:%s' %(m[:4],m[4:6],m[6:8],m[8:12])


print mac_to_ipv6_linklocal(mac)
Note that the purpose of the hex() function is to produce a hexadecimal integer literal (a string that can be used in Python source code), which is why it returns a hex value with the 0x prefix. Use the format() function instead, as it gives you much more control over the output.

For example, format(value, '012x') formats the integer value to a 12-digit hexadecimal (using lowercase digits), padding out the result to the minimum width with leading zeros as needed:

&gt;&gt;&gt; mac_value = 0x237702d2ff9b
&gt;&gt;&gt; format(mac_value, '012x')
'237702d2ff9b'
&gt;&gt;&gt; format(42, '012x')
'00000000002a'


In this case, I'd actually split out the various bytes of the MAC address binary value that make up the IPv6 parts, and use str.format() to interpolate those into the IPv6 string.

def mac_to_ipv6_linklocal(mac):
    # Remove the most common delimiters; dots, dashes, etc.
    mac_value = int(mac.translate(None, ' .:-'), 16)

    # Split out the bytes that slot into the IPv6 address
    # XOR the most significant byte with 0x02, inverting the 
    # Universal / Local bit
    high2 = mac_value &gt;&gt; 32 &amp; 0xffff ^ 0x0200
    high1 = mac_value &gt;&gt; 24 &amp; 0xff
    low1 = mac_value &gt;&gt; 16 &amp; 0xff
    low2 = mac_value &amp; 0xffff

    return 'fe80::{:04x}:{:02x}ff:fe{:02x}:{:04x}'.format(
        high2, high1, low1, low2)


I followed the PEP 8 whitespace recommendations to format the code here; whitespace around operators and after commas, for example.

In Python, we usually don't strive for shorter code; we aim for readable code. Sometimes you can express an algorithm in a very concise and readable way, but avoid code-golfing for the sake of compactness.

Python 3.3 added a ipaddress module; this module does not however include a utility to generate an IPv6 address from a given MAC address.