get_mac_address¶
- getmac.getmac.get_mac_address(interface=None, ip=None, ip6=None, hostname=None, network_request=True)[source]¶
Get a MAC address from a local interface or remote host.
Only ONE of the first four arguments may be used:
interface,ip,ip6, orhostname. If none of the arguments are selected, the default network interface for the system will be used.The MAC is usually a unicast IEEE 802 MAC-48 address.
Note
"localhost"or"127.0.0.1"will always return"00:00:00:00:00:00"Note
It is assumed that the host is using Ethernet or Wi-Fi. While other protocols such as Bluetooth may work, this has not been tested and should not be relied upon. If this functionality is needed, please open an issue or PR.
Note
Exceptions raised by methods are handled silently and returned as
None.- Parameters:
interface (
Union[str,bytes,None]) – Name of a local network interface (e.g “Ethernet 3”, “eth0”, “ens32”)ip (
Union[str,bytes,IPv4Address,IPv4Interface,IPv6Address,IPv6Interface,None]) – Canonical dotted decimal IPv4 address of a remote host (e.g192.168.0.1), or aipaddressobject (IPv4AddressorIPv4Interface). This will also acceptIPv6AddressandIPv6Interface, and treat them as ifip6argument was set instead.ip6 (
Union[str,bytes,IPv6Address,IPv6Interface,None]) – Canonical shortened IPv6 address of a remote host (e.gff02::1:ffe7:7f19), or aipaddressobject (IPv6AddressorIPv6Interface).hostname (
Union[str,bytes,None]) – DNS hostname of a remote host (e.g “router1.mycorp.com”, “localhost”)network_request (
bool) – If network requests should be made when attempting to find the MAC of a remote host. If thearpingcommand is available, this will be used. If not, a UDP packet will be sent to the remote host to populate the ARP/NDP tables for IPv4/IPv6. The port this packet is sent to can be configured using the settinggetmac.variables.Settings.PORT(by default, it’s port 55555).
- Return type:
- Returns:
Lowercase colon-separated MAC address. If no MAC was found, or an exception occurred,
Noneis returned.- Raises:
RuntimeError – If no valid methods are found for the type of MAC requested, or another critical error occurs (potentially due to a bug in getmac).