Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python
- import os
- leases = {}
- known_hosts = {}
- if os.path.isfile('/var/lib/dnsmasq/dnsmasq.leases'):
- leases_fh = open('/var/lib/dnsmasq/dnsmasq.leases', 'r')
- for line in leases_fh.readlines():
- (time, mac, ip, hostname, clientid) = line.split(' ')
- leases[mac] = {
- 'time': time,
- 'ip': ip,
- 'hostname': hostname,
- 'client_id': clientid,
- }
- leases_fh.close()
- if os.path.isfile('/etc/dnsmasq.d/static_leases'):
- known_host_fh = open('/etc/dnsmasq.d/static_leases', 'r')
- for line in known_host_fh.readlines():
- if '#' in line:
- hostname = line.split('# Host')[1].strip()
- elif '=' in line:
- if hostname == '':
- hostname = 'Unknown'
- data = line.split('=')[1]
- (mac, ip) = data.split(',')
- known_hosts[mac] = {
- 'ip': ip,
- 'hostname': hostname,
- }
- hostname = ''
- new_leases = {}
- for mac in leases:
- if mac not in known_hosts:
- new_leases[mac] = leases[mac]
- hosts = open('/etc/dnsmasq.d/static_leases', 'w')
- for mac in known_hosts:
- ip = known_hosts[mac]['ip']
- hostname = known_hosts[mac]['hostname']
- hosts.write("# Host %s\ndhcp-host=%s,%s\n" % (hostname, mac, ip))
- for mac in new_leases:
- ip = new_leases[mac]['ip']
- hostname = new_leases[mac]['hostname']
- hosts.write("# Host %s\ndhcp-host=%s,%s\n" % (hostname, mac, ip))
- hosts.close()
- print "Updating done :)"
- os.system('service dnsmasq reload')
Add Comment
Please, Sign In to add comment