A bin folder, holding helpfull scripts and commands
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

dnsleases 1.8KB

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