Open In App

Extracting MAC address using Python

Last Updated : 12 Jun, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

MAC address also known as physical address is the unique identifier that is assigned to the NIC (Network Interface Card) of the computer. NIC helps in connection of a computer with other computers in the network. MAC address is unique for all the NIC's. Uses of MAC address : 

  • Useful in places where IP address change frequently. Helps network admin. to get information regarding network traffic.
  • Helps us to configure which computers can be connected to our computers. By this way we can filter potential spam/virus attacks.
  • Helps in uniquely identifying computers from other computers around the world.

MAC-Address

Let's understand different methods to extract the MAC address.

Using getmac Module

This is the easiest method as it uses the getmac module, which is specifically designed to retrieve your MAC address with just one line of code. If you want something quick, clean and reliable, this is a great choice.

Python
from getmac import get_mac_address as gma
print(gma())

Output

30:03:c8:88:ce:07

Explanation: gma() function retrieves the MAC address of the current device by checking the system's network interfaces and returning the primary active one.

Using psutil module

This method is great if you want to see the MAC addresses of all network interfaces on your computer (like Wi-Fi, Ethernet, etc.). It uses the psutil module, which is commonly used for system monitoring and retrieving hardware/network info.

Python
import psutil

for interface in psutil.net_if_addrs():
    for snic in psutil.net_if_addrs()[interface]:
        if snic.family.name == 'AF_LINK': 
            print(f"{interface} : {snic.address}")
            break

Output

Outputbbjk

Explanation: For each network interface, it checks its addresses and looks for the one with the address family 'AF_LINK', which corresponds to the MAC address. Once found, it prints the interface name along with its MAC address and then proceeds to the next interface.

Using uuid.getnode() + re.findall()

This is the easiest method as it uses the getmac module, which is specifically made to get your MAC address with just one line of code. If you want something quick, clean, and reliable, this is the best option.

Python
import re, uuid

mac = ':'.join(re.findall('..', '%012x' % uuid.getnode()))
print(mac)

Output

92:e3:34:fe:da:7f

Explanation: uuid.getnode() returns the hardware address as a 48-bit integer, converted to a zero-padded 12-digit hex string, then split into byte pairs and joined with colons to format.

Using uuid.getnode() + format()

This method also uses only built-in Python libraries, but the formatting is done using a slightly more complex trick. It’s a bit harder to read, but it gives you a properly formatted MAC address if you're comfortable with list comprehensions and bitwise operations.

Python
import uuid

mac = ':'.join(['{:02x}'.format((uuid.getnode() >> ele) & 0xff)
                for ele in range(0, 8*6, 8)][::-1])
print(mac)

Output

92:e3:34:fe:da:7f

Explanation: getnode() returns the hardware address as a 48-bit integer, bytes are extracted by shifting and masking, formatted as hex, collected and reversed.



Next Article
Article Tags :
Practice Tags :

Similar Reads