Package: hwaddress
Lightweight python library for EUI-48, EUI-64 based hardware (MAC) addresses.Quick start & Example usageIncluded Hardware Address ClassesCommon Classmethods/Methods/PropertiesEUI PropertiesWWN PropertiesIB_GID PropertiesFactory Functionsnew_hwaddress_classget_address_factoryget_verifierQuick start & Example usageInstall hwaddress$pipinstallhwaddressImport a few hwaddress classes and create instances
by passing string representations of hardware address to them.>>>fromhwaddressimportMAC,EUI_48>>>>>>MAC('12:34:56:78:90:ab')MAC(12:34:56:78:90:ab)>>>>>>EUI_48('12-34-56-78-90-ab')EUI_48(12-34-56-78-90-ab)Strings passed to hwaddress classes do not have to conform to a given format.
All occurrences of'-', ':', '.', ' ', '0x'are removed,
and as long as the remaining characters are hexadecimal digits matching the
bit-length of the class, and instance will be created.The following list of strings are able to create instances of bothMACandEUI_48classes.>>>maclist=['12:34:56:78:90:ab','23-78-ab-CD-43-ff','0xABCDEF123456','56 78 ab cd 12 54','5432.abcd.3456','ab cdef.12-45:90']>>>>>>[MAC(mac)formacinmaclist][MAC(12:34:56:78:90:ab),MAC(23:78:ab:cd:43:ff),MAC(ab:cd:ef:12:34:56),MAC(56:78:ab:cd:12:54),MAC(54:32:ab:cd:34:56),MAC(ab:cd:ef:12:45:90)]>>>>>>[EUI_48(mac)formacinmaclist][EUI_48(12-34-56-78-90-ab),EUI_48(23-78-ab-cd-43-ff),EUI_48(ab-cd-ef-12-34-56),EUI_48(56-78-ab-cd-12-54),EUI_48(54-32-ab-cd-34-56),EUI_48(ab-cd-ef-12-45-90)]hwaddress classes have astrictclassmethod that (by default) will only
return an instance if it matches the format defined by the class.>>>MAC.strict('12:34:56:78:90:ab')MAC(12:34:56:78:90:ab)>>>>>>MAC.strict('12-34-56-78-90-ab')Traceback(mostrecentcalllast):File"<stdin>",line1,in<module>File"/Users/egeldmac/git/hwaddress/hwaddress/core.py",line228,instrictraiseValueError(f'{address}did not pass verification.')ValueError:12-34-56-78-90-abdidnotpassverification.>>>>>>EUI_48.strict('12-34-56-78-90-ab')EUI_48(12-34-56-78-90-ab)>>>>>>EUI_48.strict('12:34:56:78:90:ab')Traceback(mostrecentcalllast):File"<stdin>",line1,in<module>File"/Users/egeldmac/git/hwaddress/hwaddress/core.py",line228,instrictraiseValueError(f'{address}did not pass verification.')ValueError:12:34:56:78:90:abdidnotpassverification.hwaddress classes also have averifyclassmethod
that check if a string conforms to the format specified by the class.>>>MAC.verify('12:34:56:78:90:ab')True>>>MAC.verify('12-34-56-78-90-ab')False>>>>>>EUI_48.verify('12:34:56:78:90:ab')False>>>EUI_48.verify('12-34-56-78-90-ab')TrueThere is also aget_verifierfactory function available that,
when given hwaddress classes as arguments, will return a verifier function.
This function will return True if the address passed conforms to the format of
any of the hwaddress classes passed to get_verifier.>>>fromhwaddressimportget_verifier>>>>>>verifier=get_verifier(MAC,EUI_48)>>>>>>verifier('12:34:56:78:90:ab')True>>>verifier('12-34-56-78-90-ab')True>>>verifier('1234.5678.90ab')FalseThe resulting verifier can be used to filter a list of possible hardware
addresses or be passed to thestrictclassmethod.>>>maclist['12:34:56:78:90:ab','23-78-ab-CD-43-ff','0xABCDEF123456','56 78 ab cd 12 54','5432.abcd.3456','ab cdef.12-45:90']>>>>>>[EUI_48(mac)formacinfilter(verifier,maclist)][EUI_48(12-34-56-78-90-ab),EUI_48(23-78-ab-cd-43-ff)]>>>>>>EUI_48.strict('12:34:56:78:90:ab',verifier=verifier)EUI_48(12-34-56-78-90-ab)>>>>>>EUI_48.strict('12-34-56-78-90-ab',verifier=verifier)EUI_48(12-34-56-78-90-ab)>>>>>>EUI_48.strict('1234.5678.90ab',verifier=verifier)Traceback(mostrecentcalllast):File"<stdin>",line1,in<module>File"/Users/egeldmac/git/hwaddress/hwaddress/core.py",line228,instrictraiseValueError(f'{address}did not pass verification.')ValueError:1234.5678.90abdidnotpassverification.Included Hardware Address ClassesNameFormatPropertiesMACff:ff:ff:ff:ff:ffMAC_64ff:ff:ff:ff:ff:ff:ff:ffGUIDffffffff-ffff-ffff-ffff-ffffffffffffEUI_48ff-ff-ff-ff-ff-ffoui, oui36, cidEUI_64ff-ff-ff-ff-ff-ff-ff-ffoui, oui36, cidWWNff:ff:ff:ff:ff:ff:ff:ffnaa, ouiWWNxff:ff:ff:ff:ff:ff:ff:ff:ff:ff:ff:ff:ff:ff:ff:ffnaa, ouiIB_LID0xffffIB_GUIDffff:ffff:ffff:ffffIB_GIDffff:ffff:ffff:ffff:ffff:ffff:ffff:ffffprefix, guidCommon Classmethods/Methods/PropertiesAll classes inheriting from MAC will have the following methods, classmethos, and properties.NameTypeReturnsDescriptionverifyclassmethodboolVerify that address conforms to formatting defined by class.strictclassmethodclass instanceCreate instance only if it passes verification.formatmethodstrFormat address with given formatting options.intpropertyintInteger representation of address.hexpropertystrHexadecimal representation of address.binarypropertystrPadded binary representation of each hex digit in address.verify(address)Verify that address conforms to formatting defined by class.>>>hwaddress.MAC.verify('12:34:56:78:90:ab')True>>>hwaddress.MAC.verify('1234.5678.90ab')Falsestrict(address, verifier=None)Create object only if it passes verification.

If no verifier is passed, the classes verify classmethod will be used.>>>MAC.strict('12:34:56:78:90:ab')MAC(12:34:56:78:90:ab)>>>MAC.strict('12-34-56-78-90-ab')Traceback(mostrecentcalllast):File"<stdin>",line1,in<module>File"/Users/egeldmac/git/hwaddress/hwaddress/core.py",line228,instrictraiseValueError(f'{address}did not pass verification.')ValueError:12-34-56-78-90-abdidnotpassverification.format(self, delimiter=None, group=None, upper=None)Format address with given formatting options.

If an option is not specified,
the option defined by the class will be used

Args:
  delimiter (str): character separating hex digits.
  group (int): how many hex digits in each group.
  upper (bool): True for uppercase, False for lowercase.>>>mac=hwaddress.MAC('12:34:56:78:90:ab')>>>macMAC(12:34:56:78:90:ab)>>>str(mac)'12:34:56:78:90:ab'>>>mac.format('-')'12-34-56-78-90-ab'>>>mac.format('.',4)'1234.5678.90ab'>>>mac.format(group=4,upper=True)'1234:5678:90AB'int>>>mac.int20015998341291hex>>>mac.hex'0x1234567890ab'binary>>>mac.binary'0001 0010 0011 0100 0101 0110 0111 1000 1001 0000 1010 1011'EUI PropertiesNameReturnsDescriptionouiOIU24-bit Organizationally Unique Identifier.cidCID24-bit Company ID.oui36OUI3636-bit Organizationally Unique Identifier.WWN PropertiesNameReturnsDescriptionnaastrNetwork Address Authority.ouiOUI24-bit Organizationally Unique Identifier.IB_GID PropertiesNameReturnsDescriptionprefixIB_GID_prefix64-bit IB_GID_prefix.guidIB_GUIDEmbedded 64-bit IB_GUID.Factory Functionsnew_hwaddress_class>>>fromhwaddressimportnew_hwaddress_classget_address_factoryReturn a hwaddress object from objs tuple
depending on the address passed as an argument.>>>fromhwaddressimportget_address_factory,EUI_48,EUI_64>>>>>>hw_address=get_address_factory()>>>>>>hw_address('12:34:56:78:90:ab')MAC(12:34:56:78:90:ab)>>>hw_address('12:34:56:78:90:ab:cd:ef')MAC_64(12:34:56:78:90:ab:cd:ef)>>>>>>eui_address=get_address_factory(EUI_48,EUI_64)get_verifier>>>fromhwaddressimportMAC,EUI_48,get_verifier>>>>>>classMyMAC(MAC):..._len_=48..._del_='.'..._grp_=4...>>>>>>my_verifier=get_verifier(MAC,EUI_48,MyMAC)>>>>>>my_verifier('12:34:56:78:90:ab')True>>>my_verifier('12-34-56-78-90-ab')True>>>my_verifier('1234.5678.90ab')True>>>my_verifier('12.34.56.78.90.ab')False>>>my_verifier('1234-5678-90ab')False