2013-11-12 00:28:11 +01:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
import subprocess
|
|
|
|
import json
|
|
|
|
import re
|
|
|
|
|
|
|
|
class batman:
|
|
|
|
""" Bindings for B.A.T.M.A.N. advanced batctl tool
|
|
|
|
"""
|
|
|
|
def __init__(self, mesh_interface = "bat0"):
|
|
|
|
self.mesh_interface = mesh_interface
|
|
|
|
|
2014-01-30 22:21:15 +01:00
|
|
|
def vis_data(self,batadv_vis=False):
|
|
|
|
vds = self.vis_data_batctl_legacy()
|
|
|
|
if batadv_vis:
|
|
|
|
vds += self.vis_data_batadv_vis()
|
|
|
|
return vds
|
2014-01-24 02:53:17 +01:00
|
|
|
|
|
|
|
def vis_data_helper(self,lines):
|
2013-11-12 00:28:11 +01:00
|
|
|
vd = []
|
|
|
|
for line in lines:
|
|
|
|
try:
|
|
|
|
utf8_line = line.decode("utf-8")
|
|
|
|
vd.append(json.loads(utf8_line))
|
|
|
|
except e:
|
|
|
|
pass
|
|
|
|
return vd
|
|
|
|
|
2014-01-24 02:53:17 +01:00
|
|
|
def vis_data_batctl_legacy(self):
|
|
|
|
""" Parse "batctl -m <mesh_interface> vd json -n" into an array of dictionaries.
|
|
|
|
"""
|
|
|
|
output = subprocess.check_output(["batctl","-m",self.mesh_interface,"vd","json","-n"])
|
|
|
|
lines = output.splitlines()
|
2014-01-30 19:10:48 +01:00
|
|
|
vds = self.vis_data_helper(lines)
|
|
|
|
return vds
|
2014-01-24 02:53:17 +01:00
|
|
|
|
|
|
|
def vis_data_batadv_vis(self):
|
|
|
|
""" Parse "batadv-vis -i <mesh_interface> -f json" into an array of dictionaries.
|
|
|
|
"""
|
|
|
|
output = subprocess.check_output(["batadv-vis","-i",self.mesh_interface,"-f","json"])
|
|
|
|
lines = output.splitlines()
|
|
|
|
return self.vis_data_helper(lines)
|
|
|
|
|
2013-11-12 00:28:11 +01:00
|
|
|
def gateway_list(self):
|
|
|
|
""" Parse "batctl -m <mesh_interface> gwl -n" into an array of dictionaries.
|
|
|
|
"""
|
|
|
|
output = subprocess.check_output(["batctl","-m",self.mesh_interface,"gwl","-n"])
|
|
|
|
output_utf8 = output.decode("utf-8")
|
|
|
|
lines = output_utf8.splitlines()
|
2014-09-21 22:18:31 +02:00
|
|
|
|
|
|
|
own_mac = re.match(r"^.*MainIF/MAC: [^/]+/([0-9a-f:]+).*$", lines[0]).group(1)
|
|
|
|
|
2013-11-12 00:28:11 +01:00
|
|
|
gw = []
|
|
|
|
gw_mode = self.gateway_mode()
|
|
|
|
if gw_mode['mode'] == 'server':
|
2014-09-21 22:18:31 +02:00
|
|
|
gw.append(own_mac)
|
|
|
|
|
2013-11-12 00:28:11 +01:00
|
|
|
for line in lines:
|
2014-09-21 22:18:31 +02:00
|
|
|
gw_line = re.match(r"^(?:=>)? +([0-9a-f:]+) ", line)
|
|
|
|
if gw_line:
|
|
|
|
gw.append(gw_line.group(1))
|
|
|
|
|
2013-11-12 00:28:11 +01:00
|
|
|
return gw
|
|
|
|
|
|
|
|
def gateway_mode(self):
|
|
|
|
""" Parse "batctl -m <mesh_interface> gw"
|
|
|
|
"""
|
|
|
|
output = subprocess.check_output(["batctl","-m",self.mesh_interface,"gw"])
|
|
|
|
elements = output.decode("utf-8").split()
|
|
|
|
mode = elements[0]
|
|
|
|
if mode == "server":
|
|
|
|
return {'mode': 'server', 'bandwidth': elements[3]}
|
|
|
|
else:
|
|
|
|
return {'mode': mode}
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
bc = batman()
|
|
|
|
vd = bc.vis_data()
|
|
|
|
gw = bc.gateway_list()
|
|
|
|
for x in vd:
|
|
|
|
print(x)
|
|
|
|
print(gw)
|
|
|
|
print(bc.gateway_mode())
|