Skip to content

Intermittent failure to retrieve MAC address with the Python getmac library caused by temporarily missing ARP entries #101

@kannojsh

Description

@kannojsh

Describe the bug
The get_mac_address function from the getmac Python package is commonly used to retrieve the MAC address given an IP address or interface. There is an intermittent issue where the function fails to return a MAC address, especially in network environments where ARP (Address Resolution Protocol) entries are temporary or missing.

To Reproduce
Use the import function directly to get MAC address
mac_addr = get_mac_address(ip=ip_address, network_request=True)

Expected behavior
The expected behavior of the get_mac_address function is to reliably return the correct MAC address for a given IP address or network interface whenever it is called. It should handle temporary network conditions gracefully, such as missing or expired ARP entries, ideally by retrying or refreshing the ARP cache, so that users do not have to repeatedly call the function to obtain the MAC address

System info
(please complete the following information):

  • OS name (e.g. Windows 10 x64): Linux
  • OS Version (e.g. 1804 build 17134):
  • Python version (e.g. CPython 3.6.5 x64): 3.12.3
  • getmac version : 0.9.5

Additional context
Since ARP entries can be temporarily missing, implementing a retry mechanism with some delay between attempts often helps:

import time
from getmac import get_mac_address
def get_mac_with_retries(ip, retries=5, delay=1):
    for _ in range(retries):
        mac = get_mac_address(ip=ip)
        if mac:
            return mac
        time.sleep(delay)
    return None

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions