getmac
getmac is a cross-platform Python library designed to reliably retrieve the MAC addresses of local network interfaces and remote hosts. The current version is 0.9.5, and it maintains a regular release cadence, providing minor updates with bug fixes and improvements every few months.
Warnings
- breaking Compatibility with Python versions 2.7, 3.4, 3.5, and 3.6 will be completely removed in `getmac` 1.0.0. If your project relies on these older Python environments, you must pin your `getmac` dependency to `<1.0.0` (e.g., `getmac~=0.9.0`) to prevent breakage upon upgrade.
- gotcha Version 0.9.0 was a 'complete rewrite' of the library. While the public API aimed for backward compatibility, significant internal refactoring occurred. Users upgrading from versions prior to 0.9.0 should conduct thorough testing, as behavioral changes or differences in how MAC addresses are resolved in edge cases might occur.
- gotcha MAC address retrieval is highly dependent on the operating system, network configuration, and available system utilities. `getmac` has a history of fixing platform-specific issues (e.g., macOS ARP, BusyBox arping, IPv6 handling). Inconsistent or unexpected results may arise on certain OS/network environments; consider specifying `interface` or `ip` for more targeted queries and robust error handling.
Install
-
pip install getmac
Imports
- get_mac_address
from getmac import get_mac_address
Quickstart
from getmac import get_mac_address
# Get the MAC address of the first available local interface
mac_address = get_mac_address()
print(f"Local MAC Address: {mac_address}")
# Optionally, get MAC for a specific interface (e.g., 'eth0' or 'en0')
# interface_mac = get_mac_address(interface='eth0')
# if interface_mac:
# print(f"MAC for eth0: {interface_mac}")
# Optionally, get MAC for a remote IP address (requires ARP access)
# remote_ip = '192.168.1.1' # Replace with a reachable IP
# remote_mac = get_mac_address(ip=remote_ip)
# if remote_mac:
# print(f"MAC for {remote_ip}: {remote_mac}")