neighbours.py 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. #!/usr/bin/env python3
  2. import subprocess
  3. import re
  4. from lib.respondd import Respondd
  5. import lib.helper
  6. class Neighbours(Respondd):
  7. def __init__(self, config):
  8. Respondd.__init__(self, config)
  9. def getStationDump(self, dev_list):
  10. j = {}
  11. for dev in dev_list:
  12. try:
  13. # iw dev ibss3 station dump
  14. output = subprocess.check_output(["iw", "dev", dev, "station", "dump"])
  15. output_utf8 = output.decode("utf-8")
  16. lines = output_utf8.splitlines()
  17. mac = ""
  18. for line in lines:
  19. # Station 32:b8:c3:86:3e:e8 (on ibss3)
  20. ml = re.match(r"^Station ([0-9a-f:]+) \(on ([\w\d]+)\)", line, re.I)
  21. if ml:
  22. mac = ml.group(1)
  23. j[mac] = {}
  24. else:
  25. ml = re.match(r"^[\t ]+([^:]+):[\t ]+([^ ]+)", line, re.I)
  26. if ml:
  27. j[mac][ml.group(1)] = ml.group(2)
  28. except:
  29. pass
  30. return j
  31. def getMesh_Interfaces(self):
  32. j = {}
  33. output = subprocess.check_output(["batctl", "-m", self._config['batman'], "if"])
  34. output_utf8 = output.decode("utf-8")
  35. lines = output_utf8.splitlines()
  36. for line in lines:
  37. dev_re = re.match(r"^([^:]*)", line)
  38. dev = dev_re.group(1)
  39. j[dev] = lib.helper.getDevice_MAC(dev)
  40. return j
  41. def _get(self):
  42. j = {"batadv": {}}
  43. stationDump = None
  44. if 'mesh-wlan' in self._config:
  45. j["wifi"] = {}
  46. stationDump = self.getStationDump(self._config["mesh-wlan"])
  47. mesh_ifs = self.getMesh_Interfaces()
  48. output = subprocess.check_output(["batctl", "-m", self._config['batman'], "o", "-n"])
  49. output_utf8 = output.decode("utf-8")
  50. lines = output_utf8.splitlines()
  51. for line in lines:
  52. # * e2:ad:db:b7:66:63 2.712s (175) be:b7:25:4f:8f:96 [mesh-vpn-l2tp-1]
  53. ml = re.match(r"^[ \*\t]*([0-9a-f:]+)[ ]*([\d\.]*)s[ ]*\(([ ]*\d*)\)[ ]*([0-9a-f:]+)[ ]*\[[ ]*(.*)\]", line, re.I)
  54. if ml:
  55. dev = ml.group(5)
  56. mac_origin = ml.group(1)
  57. mac_nhop = ml.group(4)
  58. tq = ml.group(3)
  59. lastseen = ml.group(2)
  60. if mac_origin == mac_nhop:
  61. if 'mesh-wlan' in self._config and dev in self._config["mesh-wlan"] and not stationDump is None:
  62. if not mesh_ifs[dev] in j["wifi"]:
  63. j["wifi"][mesh_ifs[dev]] = {}
  64. j["wifi"][mesh_ifs[dev]]["neighbours"] = {}
  65. if mac_origin in stationDump:
  66. j["wifi"][mesh_ifs[dev]]["neighbours"][mac_origin] = {
  67. "signal": stationDump[mac_origin]["signal"],
  68. "noise": 0, # TODO: fehlt noch
  69. "inactive": stationDump[mac_origin]["inactive time"]
  70. }
  71. if dev in mesh_ifs:
  72. if not mesh_ifs[dev] in j["batadv"]:
  73. j["batadv"][mesh_ifs[dev]] = {}
  74. j["batadv"][mesh_ifs[dev]]["neighbours"] = {}
  75. j["batadv"][mesh_ifs[dev]]["neighbours"][mac_origin] = {
  76. "tq": int(tq),
  77. "lastseen": float(lastseen)
  78. }
  79. return j