Search code examples
pythonmac-address

Is there a library function to check if a MAC address is multicast?


I know that I can use the netaddr module to check if an IP address is multicast like so:

netaddr.IPAddress("192.168.1.1").is_multicast()

Before I go ahead and write my own, is there a comparable function for the various formats of MAC addresses?

For instance:

0123.4567.89ab

Solution

  • For completeness: A "function" to check this is as easy as

    >>> mac = netaddr.EUI('0123.4567.89ab')
    >>> mac_is_multicast = bool(mac.words[0] & 0b01)  # Is LSB set?
    

    Similarly, you can check if a MAC address is locally administered with

    >>> mac = netaddr.EUI('0123.4567.89ab')
    >>> mac_is_locally_administered = bool(mac.words[0] & 0b10)  # Is 2nd LSB set?