getmac

getmac is a pure-Python package to get the MAC addresses of network interfaces and hosts on the local network.

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)

It provides a Python function, get_mac_address(), and a command-line interface, getmac.

Features

  • Pure-Python (no compiled C-extensions required!)

  • Python 3.9+, on CPython and PyPy

  • Lightweight, with no dependencies and a relatively small package size

  • Supports most platforms: Windows, Linux, macOS, the BSDs, Solaris and illumos, HP-UX, Android, WSL, and Docker (see Platforms)

  • Provides a simple command line tool

  • MIT licensed!

Is getmac for you?

If you only need local interfaces and can take C extensions, netifaces or psutil will be faster. If you’re on Linux, can run as root, and C extensions are fine, arpreq can be faster too. The README has the longer version.

Quick start

Install it:

python3 -m pip install getmac

Use it from Python:

from getmac import get_mac_address

get_mac_address()                        # The default interface
get_mac_address(interface="eth0")        # A local interface, by name
get_mac_address(ip="192.168.0.1")        # A host on the LAN, by IPv4 address
get_mac_address(ip6="fe80::1")           # ...by IPv6 address
get_mac_address(hostname="home.router")  # ...or by hostname

Or from the terminal:

getmac
getmac -i eth0
getmac -4 192.168.0.1
getmac -6 fe80::1
getmac -n home.router

The result is a lowercase, colon-separated MAC address (e.g. '00:11:22:33:44:55'). If nothing was found, it’s None (on the command line, nothing is printed and the exit code is 1).

Where to go next

Getting started

Installation has every way to install getmac, and Upgrading to 1.0.0 covers what changed from 0.9.x.

Using getmac

Usage is the guide to the Python API and its settings. Command-Line Interface is the guide to the getmac command, Docker covers the Docker image, and when something goes wrong, Troubleshooting has the fixes.

Reference

API is the full API reference, generated from the code. How methods are chosen and used explains how getmac picks a method, and Customizing methods at runtime covers adding, removing, or forcing methods at runtime. Platforms is where to check what’s supported, and which commands getmac uses on each platform. Glossary defines the terms used in these docs.

Contributing

Contributing to getmac is the contribution guide, including how to set up a development environment. Architecture explains how the code is organized, Adding a new method walks through adding support for a new command or platform, and Collecting command output samples explains how to collect the command output the tests use.

Project

Changelog lists what changed in each release. Security Policy is the security policy, including how to verify releases and Docker images, and Contributor Covenant Code of Conduct covers how we treat each other.