Get ARP MAC from IP address

Submitted by:Pitter Alderson

Date added:24 December, 2013

Category:Python

This can be used to get ARP mac address from a IP address. Remember that MAC addresses are in ARP tables (volatile), that's why a ping is done first. But i don't know if timing between ping call and ARP call is enough to make sense (of pinging).

Tags: mac address , get arp

Code Snippet:

    from subprocess import Popen, PIPE
import re
IP = "192.168.10.111"
Popen(["ping", "-c 1", IP], stdout = PIPE)
pid = Popen(["arp", "-n", IP], stdout = PIPE)
s = pid.communicate()[0]
mac = re.search(r"(([a-f\d]{1,2}\:){5}[a-f\d]{1,2})", s).groups()[0]
print "%s--> %s" % (IP, mac)
 
 

Comments