Merge pull request #24785 from The-Loeki/patch-1

Small utility function for deriving IPv6 SLAAC EUI-64 identifiers
This commit is contained in:
Mike Place 2015-06-18 08:53:46 -06:00
commit 66939939c1

View File

@ -986,6 +986,27 @@ def hex2ip(hex_ip, invert=False):
hip & 255)
def mac2eui64(mac, prefix=None):
'''
Convert a MAC address to a EUI64 identifier
or, with prefix provided, a full IPv6 address
'''
# http://tools.ietf.org/html/rfc4291#section-2.5.1
eui64 = re.sub(r'[.:-]', '', mac).lower()
eui64 = eui64[0:6] + 'fffe' + eui64[6:]
eui64 = hex(int(eui64[0:2], 16) | 2)[2:].zfill(2) + eui64[2:]
if prefix is None:
return ':'.join(re.findall(r'.{4}', eui64))
else:
try:
net = ipaddress.ip_network(prefix, strict=False)
euil = int('0x{0}'.format(eui64), 16)
return '{0}/{1}'.format(net[euil], net.prefixlen)
except: # pylint: disable=bare-except
return
def active_tcp():
'''
Return a dict describing all active tcp connections as quickly as possible