5/17/2018 - 2:05 PM

Python obtener mac desde ip

Python obtener mac desde ip

# Requiere instalar netifaces
import netifaces

def mac_for_ip(ip):
    Returns a list of MACs for interfaces that have given IP, returns None if not found
    :param ip: string
    :return: if_mac|None: ip + mac
    for i in netifaces.interfaces():
        addrs = netifaces.ifaddresses(i)
            if_mac = addrs[netifaces.AF_LINK][0]['addr']
            if_ip = addrs[netifaces.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

ip = ''

# 50:a1:47:0c:ed:d8