Change language

Extracting MAC address using Python

| |

MAC address usage:

  • Useful in places where the IP address changes frequently. Helps the network administrator. get information about network traffic.
  • Helps us configure which computers can be connected to our computers. This way we can filter out potential spam / virus attacks.
  • Helps in uniquely identifying computers from other computers around the world.

This article aims to extract the MAC address of a computer using Python.

Method 1: Using uuid.getnode ( )

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

# Python program to compute
# host MAC address
# using the UUID module

 

import uuid

 
# print unique MAC value
# address using uuid and getnode () functions

print ( hex (uuid.getnode ()))

Output:

 0x163e990b db 

Disadvantage :

  • The obvious disadvantage is that the output is not formatted.

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

# Python 3 printable MAC code
# in formatted form.

 

import uuid

 
# concatenates getnode () elements after every 2 digits.

 

print ( "The MAC address in formatted way is:" , end = "")

print ( ’:’ . join ([ ’{: 02x}’ . format ((uuid.getnode ()"" ele) & amp;  0xff )

for ele in range ( 0 , 8 * 6 , 8 )] [:: - 1 ]))

Exit:

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

Disadvantage :

  • This code seems complicated.

Method 3: using getnode () + findall () + re () [to reduce complexity]

# Pyt hon 3 code for MAC printing
# formatted and simpler
# understand

 

import re, uuid

 
# concatenates getnode () elements after every 2 digits.
# using a regular expression

print ( "The MAC address in formatted and less complex way is:" , end = "")

print ( ’:’ . join (re.findall ( ’..’ , ’% 012x’ % uuid.getnode ())))

Exit:

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

Shop

Learn programming in R: courses

$

Best Python online courses for 2022

$

Best laptop for Fortnite

$

Best laptop for Excel

$

Best laptop for Solidworks

$

Best laptop for Roblox

$

Best computer for crypto mining

$

Best laptop for Sims 4

$

Latest questions

NUMPYNUMPY

psycopg2: insert multiple rows with one query

12 answers

NUMPYNUMPY

How to convert Nonetype to int or string?

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Javascript Error: IPython is not defined in JupyterLab

12 answers

News


Wiki

Python OpenCV | cv2.putText () method

numpy.arctan2 () in Python

Python | os.path.realpath () method

Python OpenCV | cv2.circle () method

Python OpenCV cv2.cvtColor () method

Python - Move item to the end of the list

time.perf_counter () function in Python

Check if one list is a subset of another in Python

Python os.path.join () method