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 address, hardware 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)
To format it as a hexadecimal value, use hex() function as shown below
import uuid mac = uuid.getnode() print(hex(mac))
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)))
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
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)
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
Hope that article was useful.
GhostofGoes / mac_addrs.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
# ************************************** |
# ** Get MAC address of a remote host ** |
def arpreq_ip ( ip ): |
# type: (str) -> Optional[str] |
import arpreq |
return arpreq . arpreq ( ‘192.168.1.1’ ) |
def scapy_ip ( ip ): |
# type: (str) -> str |
«»»Requires root permissions on POSIX platforms. |
Windows does not have this limitation.»»» |
from scapy . layers . l2 import getmacbyip |
return getmacbyip ( ip ) |
# ****************************************** |
# ****************************************** |
# ** Get MAC address of a local interface ** |
def psutil_iface ( iface ): |
# type: (str) -> Optional[str] |
import psutil |
nics = psutil . net_if_addrs () |
if iface in nics : |
nic = nics [ iface ] |
for i in nic : |
if i . family == psutil . AF_LINK : |
return i . address |
def netifaces_iface ( iface ): |
# type: (str) -> str |
import netifaces |
return netifaces . ifaddresses ( iface )[ netifaces . AF_LINK ][ 0 ][ ‘addr’ ] |
def scapy_iface ( iface ): |
# type: (str) -> str |
from scapy . layers . l2 import get_if_hwaddr |
if WINDOWS : |
from scapy . arch . windows import get_windows_if_list |
interfaces = get_windows_if_list () |
for i in interfaces : |
if any ( iface in i [ x ] for x in |
[ ‘name’ , ‘netid’ , ‘description’ , ‘win_index’ ]): |
return i [ ‘mac’ ] |
# WARNING: Do not put an ‘else’ here! |
return get_if_hwaddr ( iface ) |
# ******************************************** |
# ******************************************** |
# Determine the default interface for a system |
def netifaces_default_gateway (): |
# type: () -> str |
import netifaces |
return list ( netifaces . gateways ()[ ‘default’ ]. values ())[ 0 ][ 1 ] |
# ******************************************** |