How to get MAC address of a device in Python

In this tutorial, we will write a Python program using a special module called UUID to fetch your system’s MAC address. This module is particularly used to fetch the MAC address of any system. A media access control address is a distinctive identifier of a system that helps two or more devices connect, especially when on the same local network.

To get the MAC address of a device in Python, you can use the getnode function in the uuid module. This function generates a unique MAC address based on the system’s hardware information.

import uuid

mac_address = uuid.getnode()
print(mac_address)

The getnode function returns the MAC address as an integer. If you want to print it in the traditional MAC address format (e.g., 00:11:22:33:44:55), you can use the hex function to convert the integer to a hexadecimal string, and then use string formatting to insert the colons:

import uuid

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

Keep in mind that this method only works for the device running the Python script. If you want to get the MAC address of a different device on the same network, you will need to use a different method such as using the arp command or sending a network packet to the device and reading the MAC address from the response.

Get MAC address of a different device in Python

To use the arp command to get the MAC address of a device in Python, you can use the subprocess module to call the arp command and capture the output.

Here is an example of how to do this:

import subprocess

def get_mac_address(ip_address):
    arp_command = ['arp', '-n', ip_address]
    output = subprocess.check_output(arp_command).decode()
    mac_address = output.split()[3]
    return mac_address

mac_address = get_mac_address('192.168.1.1')
print(mac_address)

This code will call the arp command with the -n flag, which displays the ARP table with IP addresses and MAC addresses, and the IP address of the device you want to get the MAC address for. The output will be a string containing the ARP table, and the MAC address will be the fourth item in the list of words obtained by splitting the output string on whitespace.

Keep in mind that this method only works if the device you are trying to get the MAC address for is on the same network as the device running the Python script, and if the arp command is available on the system. The arp command may not work if the device has privacy settings enabled that prevent it from responding to ARP requests.

Example 1: MAC address in integer format in Python

# import the essential modules
from uuid import getnode as get_mac
# get the mac address
mac=get_mac()
# print the mac address
print("the mac address is:",mac)

Output:

the mac address is: *****6***5*6*****

For security purposes, the mac address cannot be disclosed. In this way, you can very easily figure out the MAC address of your device but this method will print a very complex MAC address. So, to reduce the complexity you can use the program given below.

 

Example 2: Get traditional MAC address format in Python

For reducing the complexity of the MAC address we have to convert it into the hexa format as shown in this code.

# import the essential modules
from uuid import getnode as get_mac
# get the mac address
mac=get_mac()
# print the mac address
print("the mac address is:",mac)
# covert it into hexa format
print(hex(mac))
# reduce the complexity
# macstring performs the clearest form of mac address and it is the correct form of valid address
# here the for loop arranges the valid mac address in orderly formats
macString=':'.join(("%012X" % mac) [i:i+2] for i in range(0,12,2))
# now print the valid mac address in the correct format
print('[' + macString + ']')

Output:

[*A:**:**:**:**:F*]

For security reasons, the MAC address cannot be exposed. So, this way you can fetch the MAC address of your computer system.

Leave a Reply

Your email address will not be published. Required fields are marked *