Skip to content

Instantly share code, notes, and snippets.

@sspreitzer
Last active November 2, 2015 12:10
Show Gist options
  • Save sspreitzer/ecc094c0e656bd4b648f to your computer and use it in GitHub Desktop.
Save sspreitzer/ecc094c0e656bd4b648f to your computer and use it in GitHub Desktop.
Python Class for Linux ARP Cache
# MIT License (c) Sascha Spreitzer, 2015
class ArpEntry(object):
def __init__(self, line):
self.ip, self.hw_type, self.flags, self.hw_address, self.mask, self.device = \
line.split()
def __str__(self):
return '%s: %s' % (self.ip, self.hw_address)
def __repr__(self):
return 'ArpEntry("%s")' % str(self)
class Arp(object):
def __init__(self):
self.sync()
def sync(self):
self.entries = []
lines = open('/proc/net/arp').readlines()[1:]
for line in lines:
self.entries.append(ArpEntry(line))
def __iter__(self):
return iter(self.entries)
def __getitem__(self, item):
self.sync()
for i in self.entries:
if i.ip == item:
return i
raise KeyError('No such ip')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment