| |
|
How to extract MAC Address by using Python define four ways to do that? Question Posted on 17 Aug 2021 Home >> Python Programming >> Python 3 >> How to extract MAC Address by using Python define four ways to do that? |
How to extract MAC Address by using Python define four ways to do that?
As we knows thet MAC Address is known as physical address which is unique identifier and that is assigned to NIC of Computer. MAC Address is useful in place of IP Address as IP Address change frequently. Below are the 3 ways to get MAC Address by using python syntax:-
(1)By using below code we will get Mac Address
(2)By using uuid.getnode() method
# Code to compute MAC address of host
# By using UUID module
import uuid
# print value of unique MAC by getnode() function
print (hex(uuid.getnode()))
But in above method format is not as per requirement we need to change that
(3)By using getnode() +format()
# Python 3 code to print MAC in formatted way.
import uuid
print ("MAC address formatted way : ", end="")
print (':'.join(['{:02x}'.format((uuid.getnode() >> ele) & 0xff)
for ele in range(0,8*6,8)][::-1]))
(4)By using Using getnode() + findall() + re()
# Python 3 code to print MAC formatted way
import re, uuid
# joins elements of getnode() after each 2 digits by using regex expression
print ("The MAC address in formatted and less complex way is : ", end="")
print (':'.join(re.findall('..', '%012x' % uuid.getnode())))
| |
|
|
|
|