Get MAC address in python

MAC address stands for Media Access Control address and it is a unique id assigned to hardware components typically to network devices.
These components are called Network Interface Controllers(NIC).
A MAC address is assigned by the manufacturer and cannot be modified. It is also called Ethernet hardware addresshardware address or physical address.

In this article, we will take a look at 2 different ways to determine MAC address in python.

Method 1: uuid module
Python uuid module has a getnode() method which returns mac address of a hardware.
Remember that a computer has many NIC or network hardwares, so each machine may have many different MAC addresses.

getnode() returns an address of only one of these hardware as a 48 bit integer. Example,

import uuid

mac = uuid.getnode()
print(mac)

This prints

84084425069681

To format it as a hexadecimal value, use hex() function as shown below

import uuid 

mac = uuid.getnode() 
print(hex(mac))

Output is

0x4c796e499471

If you want a properly formatted address as visible in terminal or command prompt, as a 12 digit value separated into blocks of 2 digits each and separated by a colon(:), use below code

import uuid
import re

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

This will print

4c:79:6e:49:94:71

To understand the above code, break it into parts as below

import uuid
import  re

mac= hex(uuid.getnode())
# remove 0x
mac=mac.replace('0x','')
# break into 2 digit list
mac=re.findall('..', mac)
# add : separator
print(':'.join(mac))

1. Determine mac address with getnode() and convert it to a hex string with hex() function.
2. Remove leading 0x from it using replace() function.
3. Break the resulting string into 2 digit values using findall() function from re module.
findall() returns a list of values.
4. Combine the elements of this list with : as separator using python string join() function.

Output of this code will be

4c:79:6e:49:94:71

Method 2: Using getmac module
Another method to get MAC address in python is using a third party package getmac.
It provides get_mac_address() function which returns MAC address. Example,

import getmac

mac = getmac.get_mac_address()
print(mac)

This prints

4c:79:6e:49:94:71

You can directly import this function as a shorter alias. This makes invoking it easier when required at multiple places. Example,

from getmac import get_mac_address as gmac

mac = gmac()

As stated earlier, a single machine has multiple NICs installed.
With get_mac_address() function, you can get the MAC address of a hardware using its name.
It also enables you to find MAC address of a particular ip as shown below.

from getmac import get_mac_address as gmac

eth_mac = gmac(interface="eth0")
ip_mac = gmac(ip="127.0.0.1")
host_mac = gmac(hostname="localhost")

Replace the hardware names as per the system on which you are running this program.

To install getmac package using pip, use below command

pip install getmac

Hope that article was useful.