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

Common xlabel/ylabel for matplotlib subplots

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

12 answers

NUMPYNUMPY

Flake8: Ignore specific warning for entire file

12 answers

NUMPYNUMPY

glob exclude pattern

12 answers

NUMPYNUMPY

How to avoid HTTP error 429 (Too Many Requests) python

12 answers

NUMPYNUMPY

Python CSV error: line contains NULL byte

12 answers

NUMPYNUMPY

csv.Error: iterator should return strings, not bytes

12 answers

News


Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

sin

How to specify multiple return types using type-hints

exp

Printing words vertically in Python

exp

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

cos

Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically