Command-Line Interface

The getmac command is primarily provided to enable debugging and troubleshooting of getmac. It also provides a handy cross-platform command line tool to get MAC addresses, in case you needed one. It’s installed along with getmac when pip installed, and can be invoked as long as your Python’s Scripts folder is on your PATH.

Basic Usage

# Print usage and command line arguments
getmac --help

# Print the current version
getmac --version

# No arguments returns the MAC address of the default network interface
getmac

# Interface names, IPv4/IPv6 addresses, or Hostnames can be specified
getmac --interface ens33
getmac --ip 192.168.0.1
getmac --ip6 fe80::1
getmac --hostname home.router

# The same, with the short forms of the arguments
getmac -i ens33
getmac -4 192.168.0.1
getmac -6 fe80::1
getmac -n home.router

# Print the name of the default network interface (e.g. "eth0"), instead of a MAC
getmac --default-interface
getmac -D

Only one of --interface, --ip, --ip6, --hostname, --default-interface, and --list-methods can be used at a time.

Running it through Python

If getmac doesn’t work, try python3 -m getmac, where python3 is whatever command you use to run Python and used to install the package. That happens when your Python environment doesn’t have the command on the PATH. It also happens on Windows, where getmac can run Windows’ own getmac.exe instead (if it prints a table that looks nothing like getmac’s, that’s why). See Troubleshooting.

On Windows, this may be py, e.g. py -m getmac, or just python, e.g. python -m getmac.

When invoking in this fashion, replace calls to getmac in the examples on this page with python3 -m getmac (or whatever invocation applies to your platform).

python3 -m getmac
python3 -m getmac --help
python3 -m getmac --version
py -m getmac
py -m getmac --help
py -m getmac --version

Output and exit codes

  • 0: the MAC was printed to stdout (also for --help and --version).

  • 1: nothing was found, or every method failed, and nothing is printed. An uncaught RuntimeError is a traceback, and exits with 1 too.

  • 2: the arguments are invalid or conflict (e.g. -i with -4). The usage message goes to stderr.

Log messages (-v) go to stderr, so stdout only ever has the MAC. This makes it easy to use in scripts:

if mac=$(getmac -i eth0); then
    echo "Found: $mac"
else
    echo "No MAC found" >&2
fi
$mac = py -m getmac -i "Ethernet 3"
if ($LASTEXITCODE -eq 0) { "Found: $mac" } else { "No MAC found" }

Nothing found and a lookup that failed look the same (exit code 1, no output). Use -v to see why.

Remote hosts

Getting the MAC address of a remote host requires the ARP table (IPv4) or NDP table (IPv6) to be populated. By default, getmac gets the host into the table with an ARP request (IPv4 only, on Windows or if arping is installed) or, if it can’t, an empty UDP packet to a high port on the host (defaults to 55555). On Linux, it reads the ARP table file first, and sends nothing if the host is already there. This can be disabled with --no-net (or -N), as shown below.

getmac --no-net --ip 192.168.0.1
getmac -N -n home.router

The host’s entry is only added to the table once it replies, which can take longer than getmac takes to check the table. Use --arp-timeout to keep checking for it for up to a number of seconds (by default, it’s checked once).

getmac --ip 192.168.0.1 --arp-timeout 1

The UDP port can be changed with --override-port.

getmac --ip 192.168.0.1 --override-port 9001

Remote hosts: IP addresses and hostnames has the details, and None for a remote host covers what to do when a host isn’t found.

Troubleshooting options

Logging and debugging

Note

When reporting an issue or asking for help, please enable verbose and the highest level of debugging: getmac -v -dddd [arguments]

Normally, no log messages are printed, just the result from the command. Adding the -v (--verbose) argument to any command will enable these messages. This is useful for debugging issues or understanding what’s happening.

getmac --verbose
getmac -v -i eth0
getmac --verbose --ip 192.168.0.1

There is also a debugging mode. There are multiple levels of debugging, up to 4. Adding the -d (--debug) argument enables debugging, and additional -d arguments increase the level of debugging. -d turns on the log messages too, so -v is optional when it’s used.

# Enable debugging at level 1
getmac -v -d

# Example with another argument
getmac -v -d -i enp11s4

# Debug level 2 (note the two 'd' characters)
getmac -v -dd -n home.router

# Debug level 3
getmac -v -ddd --ip 192.168.0.1

# Debug level 4
getmac -v -dddd -i eth0

Level 2 shows which methods were considered and which one was chosen, and level 4 shows every command that was run and its output. The levels are described in Logging and debugging.

Controlling method selection

getmac has a selection of methods used to acquire information across platforms. The methods it uses are determined by the platform and if required commands/libraries are present. This system isn’t perfect, and in some cases it can be beneficial to have control over method selection, such as in the case of debugging.

To stop getmac from using a method, for example one that gives a wrong result on your system, use --exclude-method (-E). getmac uses the next method that works instead. To exclude more than one method, give the argument more than once.

getmac -i eth0 --exclude-method SysIfaceFile
getmac --ip 192.168.0.1 -E arpfile -E ipneighborshow

--list-methods shows the methods for each type of lookup on this platform, in the order getmac tries them, and whether each one works here. It tests each method the same way a lookup does (e.g. it checks that a command exists), but it doesn’t look anything up.

getmac --list-methods

Example output on Linux:

Platform: linux

ip4:
  ArpFile                      primary
  ArpingHost                   doesn't work here (test() failed)
  IpNeighborShow               fallback
  ArpVariousArgs               doesn't work here (test() failed)

...

getmac uses the primary method for a lookup. If that method fails, getmac tries the fallback methods in order (see Falling back during a lookup). The other options change the list too, such as --override-platform, --exclude-method, and --no-net (which skips methods that send packets).

The platform detected by getmac can be overridden via --override-platform. This is useful when debugging issues or if you know a method for a different platform works on the current platform. Any value returned by platform.system() is valid.

# Force "linux" to be the "detected" platform and use "linux" methods
getmac -i eth0 --override-platform linux

# Force "windows" to be the "detected" platform and use "windows" methods
getmac --ip 192.168.0.1 --override-platform windows

A specific method can also be used with --force-method, regardless of the consequences or if it even works. The method names are listed in the API (case doesn’t matter), and --list-methods shows the ones for your platform.

getmac -v -dddd --ip 192.168.0.1 --force-method ctypeshost

Platforms lists which methods are used on each platform, and How methods are chosen and used explains how they’re chosen.

Slow commands

getmac stops a command that it runs (such as arp or ipconfig.exe) if it doesn’t finish in 15 seconds, and tries the next method. Use --command-timeout to change how long to wait, in seconds. 0 waits for as long as the command takes.

getmac --ip 192.168.0.1 --command-timeout 5

Arguments reference

getmac - CLI interface

Get MAC addresses of network interfaces or LAN hosts

getmac [-h] [--version] [-i INTERFACE | -4 IP | -6 IP6 | -n HOSTNAME | -D | --list-methods]
       [-N] [--arp-timeout SECONDS] [--command-timeout SECONDS] [-v] [-d]
       [--override-port PORT] [--override-platform PLATFORM] [--force-method METHOD]
       [-E METHOD]

getmac options

  • -h, --help - show this help message and exit

  • --version - show program’s version number and exit

  • -i INTERFACE, --interface INTERFACE - Name of a network interface on the system

  • -4 IP, --ip IP - IPv4 address of a remote host

  • -6 IP6, --ip6 IP6 - IPv6 address of a remote host

  • -n HOSTNAME, --hostname HOSTNAME - Hostname of a remote host

  • -D, --default-interface - Print the name of the default network interface, instead of a MAC

  • --list-methods - List the methods for each type of lookup on this platform, in the order getmac tries them, and whether each one works here. Then exit.

  • -N, --no-net, --no-network-requests - Do not use arping or send a UDP packet to refresh the ARP table

  • --arp-timeout SECONDS - After sending the UDP packet to refresh the ARP table, keep checking it for the host for up to this many seconds (default: 0, check once)

  • --command-timeout SECONDS - Stop a command that getmac runs (e.g. 'arp' or 'ipconfig.exe') if it takes longer than this many seconds, and try the next method (default: 15, 0 to wait for as long as it takes)

getmac troubleshooting

  • -v, --verbose - Enable logging messages (by default, only the MAC is printed to the terminal)

  • -d, --debug - Enable debugging output. Add 'd' characters to increase verbosity of output, e.g. '-dd' to set DEBUG=2.

  • --override-port PORT - Override the default UDP port used to refresh the ARP table if network requests are enabled and arping is unavailable

  • --override-platform PLATFORM - Override the platform detection with the given value (e.g. 'linux', 'windows', 'freebsd', etc.). Any value returned by platform.system() is valid.

  • --force-method METHOD - Force a specific method to be used, e.g. 'IpNeighborShow'. This will be used regardless of its method type or platform compatibility, and Method.test() will NOT be checked!

  • -E METHOD, --exclude-method METHOD - Don’t use this method, e.g. 'ArpFile'. getmac uses the next method that works instead. Give it more than once to exclude more methods.

Environment

ARP_PATH

The file to read the ARP table from, instead of /proc/net/arp. Linux only. See Environment variables for this and the other environment variables getmac uses.