Skip to content

Instantly share code, notes, and snippets.

@crhan
Created September 12, 2013 16:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save crhan/6540278 to your computer and use it in GitHub Desktop.
Save crhan/6540278 to your computer and use it in GitHub Desktop.
print DHCP leases ip and mac pair that are not expire
#!/usr/bin/env python
# coding: UTF-8
import re
import time
DHCPD_LEASES_FILE="/var/lib/dhcpd/dhcpd.leases"
# read dhcpd_leases file and match all ip,mac pair, and convert to dict type to store
dhcp_leases = open(DHCPD_LEASES_FILE,"r").read()
regex = re.compile('''lease\s
(?P<ip>\S+) # The IP
\s{.*?ends\s\d+\s
(?P<ends>[0-9:\s/]+); # lease ends
.*?hardware\sethernet\s*
(?P<mac>[a-z0-9:]+) # request MAC
.*?}''',re.DOTALL|re.X)
all_oob = regex.findall(dhcp_leases)
valid_date = lambda dhcp_lease_time: time.strptime(dhcp_lease_time,'%Y/%m/%d %H:%M:%S') > time.gmtime() # lease db use gmt time.
oob_dict = dict([[i[2],i[0]] for i in all_oob if valid_date(i[1])]) # oob_dict: { mac => IP }
def main():
for mac in oob_dict:
print oob_dict[mac],mac
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment