Open In App

Extracting MAC address using Python

Last Updated : 29 Dec, 2022
Improve
Improve
Like Article
Like
Save
Share
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 This article aims at extracting MAC address of computer using Python. Method 1 : using mac module To get the physical address of the device we use getmac module of Python.

>>>from getmac import get_mac_address as gma
>>>print(gma())
'3c:7e:94:8f:d0:34'

Method 2 : Using uuid.getnode() getnode() can be used to extract the MAC address of the computer. This function is defined in uuid module. The illustrated code given below shows how to generate a UUID for a given host, identified by its MAC address, using the uuid1() function. 

Python3




# Python Program to compute
# MAC address of host
# using UUID module
 
import uuid
 
# printing the value of unique MAC
# address using uuid and getnode() function
print (hex(uuid.getnode()))


Output :

0x163e990bdb

Drawback :

  • The visible drawback is that the output is not in the formatted form.

Method 3 : Using getnode() + format() [ for better formatting ] 

Python3




# Python 3 code to print MAC
# in formatted way.
 
import uuid
 
# joins elements of getnode() after each 2 digits.
 
print ("The MAC address in formatted way is : ", end="")
print (':'.join(['{:02x}'.format((uuid.getnode() >> ele) & 0xff)
for ele in range(0,8*6,8)][::-1]))


Output :

The MAC address in formatted way is : 00:16:3e:99:0b:db

Drawback :

  • This code appears to be complex.

Method 4 : Using getnode() + findall() + re() [ for reducing complexity ] 

Python3




# Python 3 code to print MAC
# in formatted way and easier
# to understand
 
import re, uuid
 
# joins elements of getnode() after each 2 digits.
# using regex expression
print ("The MAC address in formatted and less complex way is : ", end="")
print (':'.join(re.findall('..', '%012x' % uuid.getnode())))


Output :

The MAC address in formatted and less complex way is : 00:16:3e:99:0b:db

Method5: psutil

This code is using the psutil module to get the MAC addresses of all the network interfaces on the computer.

To install the psutil module, you can use the following command:

pip install psutil

The code first imports the psutil module and then uses the net_if_addrs() function to get a dictionary of all the network interfaces on the computer, along with their addresses. It then iterates over all the keys in the dictionary (which represent the names of the network interfaces) and checks if the interface has a valid MAC address. If it does, the code prints the name of the interface and its MAC address.
 

Python3




import psutil
 
# Iterate over all the keys in the dictionary
for interface in psutil.net_if_addrs():
    # Check if the interface has a valid MAC address
    if psutil.net_if_addrs()[interface][0].address:
        # Print the MAC address for the interface
        print(psutil.net_if_addrs()[interface][0].address)
        break


Output:  00-FF-8F-73-83-9F
 



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads