API

Full API reference for the getmac package.

In short: most people only need get_mac_address(), and maybe get_default_interface() and settings. Those three are the public API. The rest of this page is internal, and can change between releases.

Public API

These are the names in __all__ that you can import from getmac. settings is the one instance of Settings, see Configuration. (consts and gvars are internal, see below.) __version__ is the version string.

Functions

get_default_interface()

Get the name of the default network interface on the system.

get_mac_address([interface, ip, ip6, ...])

Get a MAC address from a local interface or remote host.

Variables

settings

User-configurable settings.

Methods and lookup functions

Method is the base class of every method, and the built-in methods, the caches (METHODS, METHOD_CACHE and FALLBACK_CACHE), and the lower-level lookup functions are all here. Platforms lists the methods for each platform and lookup type, in the order they’re tried. How methods are chosen and used explains how one is selected.

Get the MAC address of remote hosts or network interfaces.

It provides a platform-independent interface to get the MAC addresses of:

  • System network interfaces (by interface name)

  • Remote hosts on the local network (by IPv4/IPv6 address or hostname)

The key function is get_mac_address().

Examples
from getmac import get_mac_address

eth_mac = get_mac_address(interface="eth0")
win_mac = get_mac_address(interface="Ethernet 3")
ip_mac = get_mac_address(ip="192.168.0.1")
ip6_mac = get_mac_address(ip6="fe80::1")
host_mac = get_mac_address(hostname="home.router")
updated_mac = get_mac_address(ip="10.0.0.1", network_request=True)

Functions

get_by_method(method_type[, arg, ...])

Query for a MAC using a specific method.

get_instance_from_cache(method_type, method_name)

Get the cached instance of a named Method.

get_method_by_name(method_name)

Get a Method class from METHODS by name.

initialize_method_cache(method_type[, ...])

Initialize the method cache for the given method type.

Classes

ArpExe()

Query the Windows ARP table using arp.exe to find the MAC address of a remote host.

ArpFile()

Use the contents of /proc/net/arp to find the MAC address of a host.

ArpFreebsd()

Use the arp command to find the MAC address of a host on FreeBSD, NetBSD and DragonFly BSD.

ArpOpenbsd()

Use the arp command to find the MAC address of a host on OpenBSD.

ArpVariousArgs()

Use the arp command to find the MAC address of a host on various platforms.

ArpingHost()

Use arping command to determine the MAC of a host on Linux and Darwin (MacOS).

CtypesHost()

Uses SendARP from the Windows Iphlpapi to get the MAC address of a remote IPv4 host.

DarwinNetworksetupIface()

Use networksetup on MacOS (Darwin) to get the MAC address of a specific interface.

DefaultIfaceFreeBsd()

Determine the default interface on FreeBSD using the netstat command.

DefaultIfaceIpRoute()

Determine the default interface using the ip route command.

DefaultIfaceLinuxRouteFile()

Determine the default interface by parsing the /proc/net/route file on Linux-based platforms (including WSL).

DefaultIfaceNetsh()

Use netsh.exe to find the default interface on Windows: the interface of the IPv4 default route (0.0.0.0/0) with the lowest metric.

DefaultIfaceOpenBsd()

Determine the default interface on OpenBSD using the route command.

DefaultIfaceRouteCommand()

Determine default interface using the route -n command.

DefaultIfaceRouteGetCommand()

Determine default interface using the route -n get default command on BSD-based platforms, including Darwin (MacOS), and on Solaris and illumos.

DladmIface()

Use the dladm command to get the MAC address of a network interface on Solaris 11 and illumos (e.g. OmniOS, OpenIndiana, SmartOS).

FcntlIface()

Uses fcntl.ioctl() to get the MAC address of a network interface on Linux (including WSL).

GetmacExe()

Uses Windows-builtin getmac.exe to get a interface's MAC address.

IfconfigEther()

Determine interface MAC using ifconfig command on Darwin (MacOS) systems.

IfconfigOther()

Wild 'Shot in the Dark' attempt at using ifconfig to get interface MAC on unknown platforms.

IfconfigWithIfaceArg()

ifconfig command with the interface name as an argument (e.g. ifconfig eth0) to determine MAC address of an interface.

IpLinkIface()

Determines interface MAC using the ip link command.

IpNeighborShow()

Uses the ip neighbor show command to get the MAC address of a remote host.

IpconfigExe()

Uses ipconfig.exe to find interface MAC addresses on Windows.

LanscanIface()

Uses the lanscan command to get the MAC address of a network interface on HP-UX.

Method()

Base class defining a method to get a MAC address.

NdpNeighbors()

Use the ndp -an command to find the MAC address of an IPv6 host in the NDP neighbor cache on macOS, FreeBSD, NetBSD, OpenBSD and DragonFly BSD.

NetshNeighbors()

Use netsh.exe to find the MAC address of a host in the Windows neighbor cache (the ARP table for IPv4, and the NDP neighbor cache for IPv6).

NetstatIface()

Determines interface MAC using the netstat command.

NetstatNetToMedia()

Use netstat -p to find the MAC address of a host in the "Net to Media Table" (the ARP table for IPv4, and the NDP neighbor cache for IPv6) on Solaris and illumos.

SysIfaceFile()

Uses the contents of /sys/class/net/<iface>/address to get the MAC address of a interface.

WmicExe()

Use wmic.exe on Windows to find the MAC address of a network interface.

Variables

FALLBACK_CACHE

The methods that passed their test but weren't chosen as the primary method, in the order they appear in METHODS

METHODS

All of the methods getmac knows about, in the order they're tried.

METHOD_CACHE

Primary method to use for a given method type

Internals

Warning

Most of what this page describes lives in getmac.getmac and is an internal API that may change between releases. Use it with care.

getmac.variables and getmac.utils are internal and may change between releases. They’re documented because the methods use them, and because Customizing methods at runtime imports from them.

Global variables, constants, and settings for the getmac package.

Classes

Constants()

Platform identifiers and other constants.

Settings()

User-configurable settings.

Variables()

Things that can change.

Utility and helper functions. These are basic in functionality and should be relatively standalone. They are intended for internal use by getmac.

Functions

call_proc(executable, args)

Wrapper around subprocess.check_output() with some logging and type conversion.

check_command(command)

Check if a command exists using shutil.which().

check_path(filepath)

Check if the file pointed to by filepath exists and is readable.

clean_mac(mac)

Check and format a string result to be lowercase colon-separated MAC.

fetch_ip_using_dns()

Determine the IP address of the default network interface.

popen(command, args)

Execute a command with arguments and return the stdout (stderr is discarded).

read_file(filepath)

Open and read a file.

search(regex, text[, group_index, flags])

Search for a regular expression in a string, and return the specified group.