Guest User

Untitled

a guest
Feb 18th, 2018
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.32 KB | None | 0 0
  1. #!/usr/bin/python
  2. import os
  3. leases = {}
  4. known_hosts = {}
  5.  
  6. if os.path.isfile('/var/lib/dnsmasq/dnsmasq.leases'):
  7. leases_fh = open('/var/lib/dnsmasq/dnsmasq.leases', 'r')
  8. for line in leases_fh.readlines():
  9. (time, mac, ip, hostname, clientid) = line.split(' ')
  10. leases[mac] = {
  11. 'time': time,
  12. 'ip': ip,
  13. 'hostname': hostname,
  14. 'client_id': clientid,
  15. }
  16.  
  17. leases_fh.close()
  18.  
  19. if os.path.isfile('/etc/dnsmasq.d/static_leases'):
  20. known_host_fh = open('/etc/dnsmasq.d/static_leases', 'r')
  21. for line in known_host_fh.readlines():
  22. if '#' in line:
  23. hostname = line.split('# Host')[1].strip()
  24. elif '=' in line:
  25. if hostname == '':
  26. hostname = 'Unknown'
  27. data = line.split('=')[1]
  28. (mac, ip) = data.split(',')
  29. known_hosts[mac] = {
  30. 'ip': ip,
  31. 'hostname': hostname,
  32. }
  33. hostname = ''
  34.  
  35. new_leases = {}
  36. for mac in leases:
  37. if mac not in known_hosts:
  38. new_leases[mac] = leases[mac]
  39.  
  40. hosts = open('/etc/dnsmasq.d/static_leases', 'w')
  41. for mac in known_hosts:
  42. ip = known_hosts[mac]['ip']
  43. hostname = known_hosts[mac]['hostname']
  44. hosts.write("# Host %s\ndhcp-host=%s,%s\n" % (hostname, mac, ip))
  45.  
  46. for mac in new_leases:
  47. ip = new_leases[mac]['ip']
  48. hostname = new_leases[mac]['hostname']
  49. hosts.write("# Host %s\ndhcp-host=%s,%s\n" % (hostname, mac, ip))
  50.  
  51. hosts.close()
  52. print "Updating done :)"
  53. os.system('service dnsmasq reload')
Add Comment
Please, Sign In to add comment