neighbours.py 2.9 KB

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