Advertisement
Guest User

Untitled

a guest
Dec 20th, 2014
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.43 KB | None | 0 0
  1. import netifaces as nif
  2.  
  3. def mac_for_ip(ip):
  4. 'Returns a list of MACs for interfaces that have given IP, returns None if not found'
  5. for i in nif.interfaces():
  6. addrs = nif.ifaddresses(i)
  7. try:
  8. if_mac = addrs[nif.AF_LINK][0]['addr']
  9. if_ip = addrs[nif.AF_INET][0]['addr']
  10. except IndexError, KeyError: #ignore ifaces that dont have MAC or IP
  11. if_mac = if_ip = None
  12. if if_ip == ip:
  13. return if_mac
  14. return None
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement