Extracting MAC address using Python
Last Updated :
12 Jun, 2025
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.

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
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.
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.
Similar Reads
Extract IP address from file using Python Let us see how to extract IP addresses from a file using Python. Algorithm :  Import the re module for regular expression.Open the file using the open() function.Read all the lines in the file and store them in a list.Declare the pattern for IP addresses. The regex pattern is :  r'(\d{1,3}\.\d{1,3
2 min read
Working with IP Addresses in Python IP (Internet Protocol) -Address is the basic fundamental concept of computer networks which provides the address assigning capabilities to a network. Python provides ipaddress module which is used to validate and categorize the IP address according to their types(IPv4 or IPv6). This module is also u
3 min read
How to Make an Email Extractor in Python? In this article, we will see how to extract all the valid emails in a text using python and regex. A regular expression shortened as regex or regexp additionally called a rational expression) is a chain of characters that outline a seek pattern. Usually, such styles are utilized by string-looking al
3 min read
Get OS name and version in Python Python programming has several modules that can be used to retrieve information about the current operating system and the version that is running on the system. In this article we will explore How to get the OS name and version in Python.Let us see a simple example to get the OS name and version in
2 min read
Extract time from datetime in Python In this article, we are going to see how to extract time from DateTime in Python. In Python, there is no such type of datatype as DateTime, first, we have to create our data into DateTime format and then we will convert our DateTime data into time. A Python module is used to convert the data into Da
4 min read
Python Slicing | Extract âkâ bits from a given position How to extract âkâ bits from a given position âpâ in a number? Examples: Input : number = 171 k = 5 p = 2 Output : The extracted number is 21 171 is represented as 10101011 in binary, so, you should get only 10101 i.e. 21. Input : number = 72 k = 5 p = 1 Output : The extracted number is 8 72 is repr
4 min read