2013-11-12 00:28:11 +01:00
|
|
|
import subprocess
|
|
|
|
import json
|
|
|
|
import re
|
|
|
|
|
|
|
|
|
2015-03-24 16:49:37 +01:00
|
|
|
class Batman(object):
|
|
|
|
"""
|
|
|
|
Bindings for B.A.T.M.A.N. Advanced
|
|
|
|
commandline interface "batctl"
|
|
|
|
"""
|
2015-03-25 13:27:54 +01:00
|
|
|
def __init__(self, mesh_interface='bat0', alfred_sockpath=None):
|
2015-03-24 16:49:37 +01:00
|
|
|
self.mesh_interface = mesh_interface
|
2015-03-25 13:27:54 +01:00
|
|
|
self.alfred_sock = alfred_sockpath
|
2014-01-24 02:53:17 +01:00
|
|
|
|
2015-03-25 14:11:00 +01:00
|
|
|
# compile regular expressions only once on startup
|
|
|
|
self.mac_addr_pattern = re.compile(r'(([a-z0-9]{2}:){5}[a-z0-9]{2})')
|
|
|
|
|
2015-04-01 01:36:29 +02:00
|
|
|
def vis_data(self):
|
|
|
|
return self.vis_data_batadv_vis()
|
2013-11-12 00:28:11 +01:00
|
|
|
|
2015-03-24 16:49:37 +01:00
|
|
|
@staticmethod
|
|
|
|
def vis_data_helper(lines):
|
|
|
|
vd_tmp = []
|
|
|
|
for line in lines:
|
|
|
|
try:
|
|
|
|
utf8_line = line.decode('utf-8')
|
|
|
|
vd_tmp.append(json.loads(utf8_line))
|
|
|
|
except UnicodeDecodeError:
|
|
|
|
pass
|
|
|
|
return vd_tmp
|
2014-01-24 02:53:17 +01:00
|
|
|
|
2015-03-24 16:49:37 +01:00
|
|
|
def vis_data_batadv_vis(self):
|
|
|
|
"""
|
2015-03-24 17:41:02 +01:00
|
|
|
Parse "batadv-vis -i <mesh_interface> -f json"
|
|
|
|
into an array of dictionaries.
|
2015-03-24 16:49:37 +01:00
|
|
|
"""
|
2015-03-25 13:27:54 +01:00
|
|
|
cmd = ['batadv-vis', '-i', self.mesh_interface, '-f', 'json']
|
|
|
|
if self.alfred_sock:
|
|
|
|
cmd.extend(['-u', self.alfred_sock])
|
|
|
|
output = subprocess.check_output(cmd)
|
2015-03-24 16:49:37 +01:00
|
|
|
lines = output.splitlines()
|
|
|
|
return self.vis_data_helper(lines)
|
2014-09-21 22:18:31 +02:00
|
|
|
|
2015-03-24 16:49:37 +01:00
|
|
|
def gateway_list(self):
|
|
|
|
"""
|
2015-03-24 17:41:02 +01:00
|
|
|
Parse "batctl -m <mesh_interface> gwl -n"
|
|
|
|
into an array of dictionaries.
|
2015-03-24 16:49:37 +01:00
|
|
|
"""
|
2015-03-24 17:41:02 +01:00
|
|
|
output = subprocess.check_output(
|
|
|
|
['batctl', '-m', self.mesh_interface, 'gwl', '-n'])
|
2015-03-24 16:49:37 +01:00
|
|
|
output_utf8 = output.decode('utf-8')
|
2015-03-25 14:11:00 +01:00
|
|
|
rows = output_utf8.splitlines()
|
2014-09-21 22:18:31 +02:00
|
|
|
|
2015-03-24 16:49:37 +01:00
|
|
|
gateways = []
|
2014-09-21 22:18:31 +02:00
|
|
|
|
2015-03-25 14:11:00 +01:00
|
|
|
# local gateway
|
|
|
|
header = rows.pop(0)
|
|
|
|
mode, bandwidth = self.gateway_mode()
|
|
|
|
if mode == 'server':
|
|
|
|
local_gw_mac = self.mac_addr_pattern.search(header).group(0)
|
|
|
|
gateways.append(local_gw_mac)
|
|
|
|
|
|
|
|
# remote gateway(s)
|
|
|
|
for row in rows:
|
|
|
|
match = self.mac_addr_pattern.search(row)
|
|
|
|
if match:
|
|
|
|
gateways.append(match.group(1))
|
2013-11-12 00:28:11 +01:00
|
|
|
|
2015-03-24 16:49:37 +01:00
|
|
|
return gateways
|
|
|
|
|
|
|
|
def gateway_mode(self):
|
|
|
|
"""
|
|
|
|
Parse "batctl -m <mesh_interface> gw"
|
2015-03-25 14:11:00 +01:00
|
|
|
return: tuple mode, bandwidth, if mode != server then bandwidth is None
|
2015-03-24 16:49:37 +01:00
|
|
|
"""
|
2015-03-24 17:41:02 +01:00
|
|
|
output = subprocess.check_output(
|
|
|
|
['batctl', '-m', self.mesh_interface, 'gw'])
|
2015-03-25 14:11:00 +01:00
|
|
|
chunks = output.decode("utf-8").split()
|
|
|
|
|
|
|
|
return chunks[0], chunks[3] if 3 in chunks else None
|
2013-11-12 00:28:11 +01:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2015-03-24 16:49:37 +01:00
|
|
|
bc = Batman()
|
|
|
|
vd = bc.vis_data()
|
|
|
|
gw = bc.gateway_list()
|
|
|
|
for x in vd:
|
|
|
|
print(x)
|
|
|
|
print(gw)
|
|
|
|
print(bc.gateway_mode())
|