I have a Python script running on Linux that needs to repeatedly and reliably read the ARP table, in order to check some IP-MAC associations. I'm evaluating the possible options, and wondering which one of these is leanest/cleanest - primarily in terms of performance and overhead.
The following are currently on my radar:
arp -an using subprocess and parse the output/proc/net/arp and parse the output (similar to #1 but different way to get the underlying data; libraries like python_arptable do exactly that)SIOCGARP ioctl system call to poll the ARP table (per http://man7.org/linux/man-pages/man7/arp.7.html). I don't yet have the Python implementation for this, but believe it would be feasible.ip neighbor list using subprocess and parse the outputIf I'm not mistaken, options #1 and #2 read the table directly from the kernel cache, and #4 and #5 use netlink calls; I'm not too sure where #3 falls.
For some time I was doing #5 - specifically using pyroute2 - but experienced some reliability issues; e.g. ARP entries that definitely exist not being listed. I was left with the sense that pyroute2 is potentially a bit too powerful for the simple task at hand here. I'm currently doing #2 and it seems to be working fine for now - but wondering if there's a better way.
Although my implementation language is Python, I believe the question is not strongly language-specific. Would love some pointers on the pros and cons of the above. Specifically:
/proc? (option #1 vs #2)I found this question when looking for a similar solution. This is a MRE of what I settled on:
with open("/proc/net/arp") as arpfile:
for entry in arpfile.readlines():
if not entry.startswith("IP address"):
ip = entry.split()[0]
mac = entry.split()[3]
mac_if = entry.split()[5]
print("IP:", ip)
print("mac:", mac)
print("mac_if:", mac_if)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With