A bin folder, holding helpfull scripts and commands
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

dnsleases 2.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. #!/bin/python3
  2. #
  3. import subprocess
  4. import time
  5. class host(object):
  6. def __init__(self):
  7. self.mac = ''
  8. self.ip = ''
  9. self.vlan = 0
  10. self.state = ''
  11. self.hostname = ''
  12. self.lease_tm = 0
  13. def sortcriteria(h):
  14. rv = h.hostname if h.hostname != '*' else '~'
  15. rv += ".".join(["%03d" % int(v) for v in h.ip.split('.')])
  16. return rv.lower()
  17. def __str__(self):
  18. return " * %-16s - %17s - %-10s - %15s - %s" % (self.ip, self.mac, self.state, time.asctime(time.localtime(self.lease_tm)), self.hostname)
  19. class hostlist(dict):
  20. def __init__(self):
  21. dict.__init__(self)
  22. def append(self, host):
  23. if host.mac in self:
  24. if host.state < self[host.mac].state:
  25. self[host.mac] = host
  26. else:
  27. self[host.mac] = host
  28. def vlan_list(self, vlan):
  29. rv = hostlist()
  30. for h in self.values():
  31. if h.vlan == vlan:
  32. rv.append(h)
  33. return rv
  34. def __str__(self):
  35. entries = list(self.values())
  36. entries.sort(key=host.sortcriteria)
  37. rv = ""
  38. for h in entries:
  39. rv += str(h) + '\n'
  40. return rv
  41. hl = hostlist()
  42. #
  43. # ip n
  44. #
  45. for line in subprocess.check_output(['ip', 'n']).decode('utf-8').split('\n'):
  46. data = line.split(" ")
  47. h = host()
  48. h.ip = data[0]
  49. try:
  50. h.vlan = int(h.ip.split('.')[2])
  51. except IndexError:
  52. pass
  53. if len(data) == 6:
  54. h. mac = data[4]
  55. h.state = data[5]
  56. else:
  57. continue
  58. hl.append(h)
  59. #
  60. # leases
  61. #
  62. with open('/var/lib/misc/dnsmasq.leases', 'r') as fh:
  63. for line in fh.read().split('\n'):
  64. data = line.split(' ')
  65. if len(data) == 5:
  66. h = hl.get(data[1], host())
  67. h.lease_tm = int(data[0])
  68. h.mac = data[1]
  69. h.ip = data[2]
  70. try:
  71. h.vlan = int(h.ip.split('.')[2])
  72. except IndexError:
  73. pass
  74. h.hostname = data[3]
  75. if h.mac not in hl:
  76. hl.append(h)
  77. for vlan in [20, 30, 40, 50, 60, 90]:
  78. print("Hosts in VLAN %d" % vlan)
  79. print(hl.vlan_list(vlan))
  80. print()