Simple we can find mac addresses by socket connection and
netifaces library. first you have to download this library from given below link according to python version and OS .
https://pypi.python.org/pypi/netifaces#downloads
https://pypi.python.org/pypi/netifaces#downloads
from socket import *
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
print mac_for_ip(gethostbyname_ex(gethostname())[2][0])
No comments:
Post a Comment