Usage¶
getmac provides a Python function, get_mac_address(), and a command-line interface, getmac. This page covers using getmac from Python as a library. The command-line interface is documented in Command-Line Interface.
If a MAC address was found, get_mac_address() returns it as a lowercase, colon-separated string, e.g. '00:11:22:33:44:55'. If no MAC was found, None is returned. See Errors and exceptions for the exceptions it can raise.
Examples¶
Here’s the basic idea. With no arguments, you get the MAC of the default network interface. Pass the name of an interface to get its MAC, or the address of a host on your network to get that host’s:
>>> from getmac import get_mac_address
>>> get_mac_address()
'00:11:22:33:44:55'
>>> get_mac_address(interface="eth0")
'00:11:22:33:44:55'
>>> get_mac_address(ip="192.168.0.1")
'66:77:88:99:aa:bb'
Only one of interface, ip, ip6, and hostname can be used at a time.
Default interface¶
With no arguments, you get the MAC of the system’s default network interface, i.e. the one with the default route:
from getmac import get_mac_address
get_mac_address()
If the default interface can’t be determined (for example, if there aren’t any routes) or doesn’t have a MAC (for example, a VPN tunnel), then the first interface that has a MAC and isn’t a loopback interface is used. If there isn’t one, None is returned. On Windows, only the default interface is used. With network requests enabled, getmac first looks up the IP of the default interface as a host (see Windows).
To get the name of the default interface, use get_default_interface(). It finds the default interface without the fallbacks that get_mac_address() applies later (the first interface with a MAC, the Windows host lookup, and the remembered default). So the MAC from get_mac_address() can belong to a different interface than the name you get here. See how the default interface is found.
from getmac import get_default_interface
print(get_default_interface()) # e.g. "eth0", or "Ethernet 2" on Windows
Interface names¶
from getmac import get_mac_address
# Linux example
get_mac_address(interface="eth0")
# Windows example
get_mac_address(interface="Ethernet 3")
On Windows, the name must match the whole connection name (Ethernet 3) or adapter description (Intel(R) Ethernet Connection I217-V). Case doesn’t matter. On Unix, names are case-sensitive, and an interface that doesn’t exist (or doesn’t have a MAC, like a tunnel) gives you None.
Remote hosts: IP addresses and hostnames¶
“Remote hosts” are hosts in your local layer 2 network, also known as a “broadcast domain”, “LAN”, or “VLAN”. As far as I know, there is not a reliable method to get a MAC address for a remote host external to the LAN, since MACs don’t cross routers. If you know any methods otherwise, please open an issue on GitHub or shoot me an email, I’d love to be wrong about this.
from getmac import get_mac_address
# IPv4 example
get_mac_address(ip="192.168.0.1")
# IPv6 example
get_mac_address(ip6="fe80::1")
# Hostname example
get_mac_address(hostname="home.router")
How getmac finds a remote host¶
A hostname is resolved to an IPv4 address with DNS, then looked up like ip. hostname="localhost" (-n localhost on the command line) and ip="127.0.0.1" (-4 127.0.0.1) return "00:00:00:00:00:00", the MAC of the loopback interface, without looking anything up. Other spellings, such as ::1, 127.0.0.2, LOCALHOST, or the host’s own name, aren’t short-circuited, and return None.
Finding the MAC of a remote host means finding it in the system’s ARP table (IPv4) or NDP table (IPv6). The host is only in the table if the system has talked to it recently, so by default (network_request=True) getmac tries to get it in there first:
On Linux, getmac reads the ARP table file first. If it has a complete entry for the host, nothing is sent on the network.
If it doesn’t, then on Windows, or on Linux and macOS with
arpinginstalled, getmac sends an ARP request to the host and waits for the reply (IPv4 only).If neither applies, getmac sends an empty UDP packet to the host on port
55555(PORT). To send the packet, the system has to resolve the host’s MAC with ARP (or NDP), which adds it to the table. IPv6 hosts always use this step.
Reading the tables doesn’t need special privileges, but arping usually does (root, or CAP_NET_RAW). If it’s installed and getmac can’t use it, the lookup returns None. See Do I need root or admin?.
If a remote host gives you None and you think it shouldn’t, see None for a remote host.
Turning off network requests¶
Set network_request=False to only use what’s already in the table, for example if you have requirements to prevent network traffic:
from getmac import get_mac_address
get_mac_address(ip="10.0.0.1", network_request=False)
What network_request does depends on the argument. With interface, nothing, so it’s safe to set in a script. With ip, ip6, and hostname, it allows the ARP request or UDP packet described in How getmac finds a remote host. With no arguments, it only matters on Windows, where it allows connecting the UDP socket to find the default interface’s IP. IPv4 lookups and network requests has the exact sequence of what getmac does.
Waiting for slow hosts¶
The host’s entry is only added to the table once it replies, which can take longer than getmac takes to check the table. To keep checking for it for up to a number of seconds, set ARP_TIMEOUT (--arp-timeout on the command line). Lookups of hosts that don’t reply take this much longer. This doesn’t apply when getmac sends an ARP request itself, since it waits for the reply.
from getmac import get_mac_address, settings
settings.ARP_TIMEOUT = 1.0 # Default is 0, check once
get_mac_address(ip="10.0.0.1")
IPv6 link-local addresses and zone IDs¶
IPv6 link-local addresses (fe80::/10) only mean something together with an interface, so they can carry a zone: the interface’s name (ip6="fe80::1%en0") or its index (ip6="fe80::1%2"). On macOS and the BSDs (NdpNeighbors) and Solaris and illumos (NetstatNetToMedia), getmac uses the zone to pick the right entry when the same address is on more than one link. On Linux and Windows, pass the address without a zone, since ip neighbor show and netsh don’t understand it today.
Using ipaddress objects¶
The ip and ip6 arguments also accept objects from the standard library’s ipaddress module:
ipacceptsIPv4AddressandIPv4Interfaceobjects.ipalso acceptsIPv6AddressandIPv6Interfaceobjects, and treats them as IPv6 addresses, as ifip6was used. A string passed toipis always treated as IPv4.ip6acceptsIPv6AddressandIPv6Interfaceobjects.Networks, such as
IPv4Network, raise aValueError. They’re networks, not hosts, and passing them makes no sense.
from getmac import get_mac_address
from ipaddress import ip_address, IPv4Interface, IPv6Interface
ipv4_addr = ip_address("192.168.0.1")
print(get_mac_address(ip=ipv4_addr))
ipv6_addr = ip_address("fe80::1")
print(get_mac_address(ip=ipv6_addr))
ipv4_iface = IPv4Interface("192.168.0.1/24")
print(get_mac_address(ip=ipv4_iface))
ipv6_iface = IPv6Interface("fe80::1/64")
print(get_mac_address(ip=ipv6_iface))
All four lookup arguments also accept bytes, which are decoded as UTF-8.
Configuration¶
Settings that affect the behavior of getmac are attributes of getmac.settings (an instance of Settings, which documents each one in full). Set them before calling get_mac_address(). Logging is the exception, it’s handled by Python’s logging module (see Logging and debugging). The following settings are available, each with its command-line flag:
DEBUG: integer that controls debugging output. The higher the value, the more output you get (0is the default,4is roughly the highest). Command line:-d, repeated once per level.PORT: the UDP port used to populate the ARP table (IPv4) or NDP table (IPv6) when looking up MACs for hosts or IPs. Default is55555. Command line:--override-port.ARP_TIMEOUT: how long to keep checking the ARP table (IPv4) or NDP table (IPv6) for a host after sending the UDP packet, in seconds.0(the default) checks once, without waiting. Command line:--arp-timeout.COMMAND_TIMEOUT: how long to wait for a command that getmac runs (such asarporipconfig.exe) to finish, in seconds. A command that takes longer is stopped, and the next method is tried. The default is 15 seconds, andNoneor0waits for as long as the command takes. Command line:--command-timeout.OVERRIDE_PLATFORM: override the platform detection with the given value (e.g."linux","windows","freebsd", etc). Any value returned byplatform.system()is valid. Set it before the first lookup. Command line:--override-platform.FORCE_METHOD: name of a method to use for every lookup, e.g."IpNeighborShow"forIpNeighborShow. It’s used regardless of its method type or platform compatibility, andtest()is NOT checked! The list of available methods is ingetmac.getmac.METHODSand the API. Command line:--force-method.
The last two are for debugging and testing. How methods are chosen and used explains how they interact with method selection.
Here’s an example of changing some settings at runtime:
from getmac import get_mac_address, settings
# Enable debugging
settings.DEBUG = 2 # DEBUG level 2
print(get_mac_address(interface="Ethernet 3"))
# Change the UDP port used for updating the ARP table (UDP packet)
settings.PORT = 44444 # Default port for getmac is 55555
print(get_mac_address(ip="192.168.0.1", network_request=True))
Logging and debugging¶
Runtime messages and errors are recorded to the getmac logger using Python’s logging module. Nothing is shown until logging is configured, e.g. with logging.basicConfig(). DEBUG controls how much detail is logged, and its documentation describes each level.
import logging
from getmac import get_mac_address, settings
logging.basicConfig(format="%(levelname)-8s %(message)s", level=logging.DEBUG)
settings.DEBUG = 2
get_mac_address(interface="lo")
When reporting an issue, use level 4. Logging and debug output shows what the output looks like, and Troubleshooting covers the common problems.
Environment variables¶
A few environment variables change how getmac works. They’re read once, when getmac is imported, so set them in the process environment (or before import getmac).
ARP_PATHThe file
ArpFilereads the ARP table from,/proc/net/arpby default. It only applies on Linux (including WSL2 and Android). It’s mostly useful in containers, e.g. to read the host’s table (see Docker).ANDROID_STORAGEUsed to detect Android. You don’t set this, Android does.
PATHgetmac copies
PATHwhen it’s imported and uses the copy to find commands. On POSIX systems,/sbinand/usr/sbinare added to the copy, sincearp,ipand friends often live there. On Windows, anyScriptsdirectories are removed from it, so the real Windowsgetmac.exeis run instead of this package’s script. It doesn’t change your shell’sPATH, and changingos.environafter the import has no effect.LC_ALLgetmac sets
LC_ALL=Cfor every command it runs, so the output isn’t translated and can be parsed. That only has an effect on POSIX systems. On Windows, command output is still translated, see theipconfigentry in Troubleshooting.
Errors and exceptions¶
A lookup that finds nothing returns
None. This covers an interface that doesn’t exist, a host that isn’t in the ARP/NDP table, and a hostname that doesn’t resolve. Exceptions raised by the platform-specific methods are caught and logged, and the next method is tried (see Falling back during a lookup). The one exception is a method forced withFORCE_METHOD, whose exceptions are not caught.ValueErrormeans a bad argument: anipaddressnetwork (getmac needs a host, not a network), or an unsupported type such asint.RuntimeWarningis shown (usingwarnings, and logged) when getmac has no methods for your platform and falls back to the generic ones (see Other platforms). It’s only an exception if warnings are turned into errors, e.g. withpython -W erroror pytest’sfilterwarnings = error.RuntimeErrormeans getmac has no working method for this kind of lookup on this platform. Either no methods match the type of request and platform, or all of them failed their test (in other words, none of them work). Retrying won’t help, it’s an environment problem or a bug worth reporting. See Troubleshooting. This is new in 1.0.0, 0.9.x returnedNonewith a warning.
Limitations¶
The physical transport is assumed to be Ethernet or Wi-Fi. Others, such as Bluetooth or cellular, may work, but they’re untested. I’m definitely open to pull requests or issues related to this, including error reports.
Remote hosts have to be on your local network (see Remote hosts: IP addresses and hostnames).
The accuracy and reliability of the MAC address retrieval may vary depending on the network configuration, the target host’s settings, and other factors.
The rest, such as Android’s restrictions, local IPs on Linux, and hostnames for IPv6 devices, are listed per platform in Known issues.