Using my answer from here: https://stackoverflow.com/a/167189/
It would be important to know which iface you need a MAC for, as many may exist (bluetooth, several nics, etc.).
This task is when you know the IP address of the iface, which requires a MAC, using netifaces (available in PyPI):
import netifaces as nif def mac_for_ip(ip): 'Returns a list of MACs for interfaces that have given IP, returns None if not found' for i in nif.interfaces(): addrs = nif.ifaddresses(i) try: if_mac = addrs[nif.AF_LINK][0]['addr'] if_ip = addrs[nif.AF_INET][0]['addr'] except IndexError, KeyError: #ignore ifaces that dont have MAC or IP if_mac = if_ip = None if if_ip == ip: return if_mac return None
Testing:
>>> mac_for_ip('169.254.90.191') '2c:41:38:0a:94:8b'
kursancew Aug 03 '13 at 10:40 on 2013-08-03 10:40
source share