Mac Address¶
netutils.mac
¶
Functions for working with MAC addresses.
get_oui(mac)
¶
Returns the company name for a given mac as defined by the IEEE.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
mac |
str
|
A MAC address in string format that matches one of the defined regex patterns. |
required |
Returns:
Type | Description |
---|---|
str
|
The name of the company the mac is related to. |
Examples:
>>> from netutils.mac import get_oui
>>> from netutils.data_files.oui_mappings import OUI_MAPPINGS
>>> get_oui("cc.79.d7.dd.ee.ff")
'Cisco Systems, Inc'
>>>
Source code in netutils/mac.py
is_valid_mac(mac)
¶
Verifies whether or not a string is a valid MAC address.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
mac |
str
|
A MAC address in string format that matches one of the defined regex patterns. |
required |
Returns:
Type | Description |
---|---|
bool
|
The result as to whether or not the string is a valid MAC address. |
Examples:
>>> from netutils.mac import is_valid_mac
>>> is_valid_mac("aa.bb.cc.dd.ee.ff")
True
>>> is_valid_mac("aa.bb.cc.dd.ee.gg")
False
>>>
Source code in netutils/mac.py
mac_normalize(mac)
¶
Return the MAC address with only the address, and no special characters.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
mac |
str
|
A MAC address in string format that matches one of the defined regex patterns. |
required |
Returns:
Type | Description |
---|---|
str
|
The MAC address with no special characters. |
Examples:
>>> from netutils.mac import mac_normalize
>>> mac_normalize("aa.bb.cc.dd.ee.ff")
'aabbccddeeff'
>>>
Source code in netutils/mac.py
mac_to_format(mac, frmt='MAC_NO_SPECIAL')
¶
Converts the MAC address to a specific format.
The frmt
is a combination delimiter (COLON, DASH, DOT) and number
of characters (TWO, FOUR), e.g. f'MAC_{delimiter}_{char_num}' or if no
special characters as MAC_NO_SPECIAL
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
mac |
str
|
A MAC address in string format that matches one of the defined regex patterns. |
required |
frmt |
str
|
A format in which the MAC address should be returned in, one of MAC_COLON_TWO, MAC_COLON_FOUR, MAC_DASH_TWO, MAC_DASH_FOUR, MAC_DOT_TWO, MAC_DOT_FOUR, MAC_NO_SPECIAL. |
'MAC_NO_SPECIAL'
|
Returns:
Type | Description |
---|---|
str
|
A MAC address in the specified format. |
Examples:
>>> from netutils.mac import mac_to_format
>>> mac_to_format("aa.bb.cc.dd.ee.ff", "MAC_DASH_FOUR")
'aabb-ccdd-eeff'
>>>
Source code in netutils/mac.py
mac_to_int(mac)
¶
Converts the MAC address to an integer.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
mac |
str
|
A MAC address in string format that matches one of the defined regex patterns. |
required |
Returns:
Type | Description |
---|---|
int
|
The valid MAC address converted to an integer. |
Examples:
Source code in netutils/mac.py
mac_type(mac)
¶
Retuns the "type" of MAC address, as defined by the regex pattern names.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
mac |
str
|
A MAC address in string format that matches one of the defined regex patterns. |
required |
Returns:
Type | Description |
---|---|
Optional[str]
|
The regex pattern type of the MAC address. |
Examples:
>>> from netutils.mac import mac_type
>>> mac_type("aa.bb.cc.dd.ee.ff")
'MAC_DOT_TWO'
>>> mac_type("aabb.ccdd.eeff")
'MAC_DOT_FOUR'
>>>