neighbours.py 3.0 KB

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