Get an Unicast IEEE 802 MAC-48 address from a local interface or remote host. Only ONE of the first four arguments may be used (``interface``,``ip``, ``ip6``, or ``hostname``). If none of the arguments are selected, the default network interface for the system will be used.
( # noqa: C901
interface=None, ip=None, ip6=None, hostname=None, network_request=True
)
| 1690 | |
| 1691 | |
| 1692 | def get_mac_address( # noqa: C901 |
| 1693 | interface=None, ip=None, ip6=None, hostname=None, network_request=True |
| 1694 | ): |
| 1695 | # type: (Optional[str], Optional[str], Optional[str], Optional[str], bool) -> Optional[str] |
| 1696 | """ |
| 1697 | Get an Unicast IEEE 802 MAC-48 address from a local interface or remote host. |
| 1698 | |
| 1699 | Only ONE of the first four arguments may be used |
| 1700 | (``interface``,``ip``, ``ip6``, or ``hostname``). |
| 1701 | If none of the arguments are selected, the default network interface for |
| 1702 | the system will be used. |
| 1703 | |
| 1704 | .. warning:: |
| 1705 | In getmac 1.0.0, exceptions will be raised if the method cache initialization fails |
| 1706 | (in other words, if there are no valid methods found for the type of MAC requested). |
| 1707 | |
| 1708 | .. warning:: |
| 1709 | You MUST provide :class:`str` typed arguments, REGARDLESS of Python version |
| 1710 | |
| 1711 | .. note:: |
| 1712 | ``"localhost"`` or ``"127.0.0.1"`` will always return ``"00:00:00:00:00:00"`` |
| 1713 | |
| 1714 | .. note:: |
| 1715 | It is assumed that you are using Ethernet or Wi-Fi. While other protocols |
| 1716 | such as Bluetooth may work, this has not been tested and should not be |
| 1717 | relied upon. If you need this functionality, please open an issue |
| 1718 | (or better yet, a Pull Request ;))! |
| 1719 | |
| 1720 | .. note:: |
| 1721 | Exceptions raised by methods are handled silently and returned as :obj:`None`. |
| 1722 | |
| 1723 | Args: |
| 1724 | interface (str): Name of a local network interface (e.g "Ethernet 3", "eth0", "ens32") |
| 1725 | ip (str): Canonical dotted decimal IPv4 address of a remote host (e.g ``192.168.0.1``) |
| 1726 | ip6 (str): Canonical shortened IPv6 address of a remote host (e.g ``ff02::1:ffe7:7f19``) |
| 1727 | hostname (str): DNS hostname of a remote host (e.g "router1.mycorp.com", "localhost") |
| 1728 | network_request (bool): If network requests should be made when attempting to find the |
| 1729 | MAC of a remote host. If the ``arping`` command is available, this will be used. |
| 1730 | If not, a UDP packet will be sent to the remote host to populate |
| 1731 | the ARP/NDP tables for IPv4/IPv6. The port this packet is sent to can |
| 1732 | be configured using the module variable ``getmac.PORT``. |
| 1733 | |
| 1734 | Returns: |
| 1735 | Lowercase colon-separated MAC address, or :obj:`None` if one could not be |
| 1736 | found or there was an error. |
| 1737 | """ # noqa: E501 |
| 1738 | |
| 1739 | if DEBUG: |
| 1740 | import timeit |
| 1741 | |
| 1742 | start_time = timeit.default_timer() |
| 1743 | |
| 1744 | if PY2 or (sys.version_info[0] == 3 and sys.version_info[1] < 7): |
| 1745 | global WARNED_UNSUPPORTED_PYTHONS |
| 1746 | if not WARNED_UNSUPPORTED_PYTHONS: |
| 1747 | warning_string = ( |
| 1748 | "Support for Python versions < 3.7 is deprecated and will be " |
| 1749 | "removed in getmac 1.0.0. If you are stuck on an unsupported " |
no test coverage detected