Skip to content

Instantly share code, notes, and snippets.

@flagranterror
Created September 4, 2013 03:09
Show Gist options
  • Save flagranterror/6432364 to your computer and use it in GitHub Desktop.
Save flagranterror/6432364 to your computer and use it in GitHub Desktop.
Search a DHCP log file for a list of MAC addresses and print the last (up to) 10 lease times
#!/usr/bin/python
import re
from sys import argv
m = open('macs.txt', 'r').read()
log_file = ''
if '-z' in argv:
from gzip import open
log_file = argv[2]
else:
log_file = argv[1]
macs = m.split('\n')
mac_dict = {}
for mac in macs:
mac_dict[mac] = []
log = open(log_file, 'r')
for line in log:
if re.search('DHCPACK', line):
words = line.split(' ')
if words[9] in macs:
mac_dict[words[9]].append(
"{0} {1},{2}".format(words[0], words[1], words[2])
)
for k,v in mac_dict.iteritems():
if len(v) > 10:
print "{0}:\n,{1}".format(k, "\n,".join(v[-10:]))
elif len(v) > 0:
print "{0}:\n,{1}".format(k, "\n,".join(v))
else:
print "{0}\n,None".format(k)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment