simplify mark_gateway

This commit is contained in:
Nils Schneider 2014-09-21 22:18:31 +02:00
parent 878267ca0e
commit 6e101bc6de
3 changed files with 18 additions and 31 deletions

View file

@ -29,6 +29,7 @@ parser.add_argument('-a', '--aliases',
metavar='FILE') metavar='FILE')
parser.add_argument('-m', '--mesh', action='append', parser.add_argument('-m', '--mesh', action='append',
default=["bat0"],
help='batman mesh interface') help='batman mesh interface')
parser.add_argument('-A', '--alfred', action='store_true', parser.add_argument('-A', '--alfred', action='store_true',
@ -43,17 +44,11 @@ options = vars(args)
db = NodeDB(int(time.time())) db = NodeDB(int(time.time()))
if options['mesh']:
for mesh_interface in options['mesh']: for mesh_interface in options['mesh']:
bm = batman(mesh_interface) bm = batman(mesh_interface)
db.parse_vis_data(bm.vis_data(options['alfred'])) db.parse_vis_data(bm.vis_data(options['alfred']))
for gw in bm.gateway_list(): for gw in bm.gateway_list():
db.mark_gateways(gw['mac']) db.mark_gateway(gw)
else:
bm = batman()
db.parse_vis_data(bm.vis_data(options['alfred']))
for gw in bm.gateway_list():
db.mark_gateways([gw['mac']])
if options['aliases']: if options['aliases']:
for aliases in options['aliases']: for aliases in options['aliases']:

View file

@ -45,22 +45,20 @@ class batman:
""" """
output = subprocess.check_output(["batctl","-m",self.mesh_interface,"gwl","-n"]) output = subprocess.check_output(["batctl","-m",self.mesh_interface,"gwl","-n"])
output_utf8 = output.decode("utf-8") output_utf8 = output.decode("utf-8")
# TODO Parse information
lines = output_utf8.splitlines() lines = output_utf8.splitlines()
own_mac = re.match(r"^.*MainIF/MAC: [^/]+/([0-9a-f:]+).*$", lines[0]).group(1) own_mac = re.match(r"^.*MainIF/MAC: [^/]+/([0-9a-f:]+).*$", lines[0]).group(1)
# Remove header line
del lines[0]
# Fill gateway list
gw = [] gw = []
gw_mode = self.gateway_mode() gw_mode = self.gateway_mode()
if gw_mode['mode'] == 'server': if gw_mode['mode'] == 'server':
gw.append({'mac': own_mac, 'bandwidth': gw_mode['bandwidth']}) gw.append(own_mac)
for line in lines: for line in lines:
gw_line = line.split() gw_line = re.match(r"^(?:=>)? +([0-9a-f:]+) ", line)
if (gw_line[0] == 'No'): if gw_line:
continue gw.append(gw_line.group(1))
# When in client gateway mode maybe gw_line[0] is not the right.
gw.append({'mac':gw_line[0], 'bandwidth': gw_line[-1]})
return gw return gw
def gateway_mode(self): def gateway_mode(self):

View file

@ -224,18 +224,12 @@ class NodeDB:
if 'id' in alias: if 'id' in alias:
node.id = alias['id'] node.id = alias['id']
# list of macs def mark_gateway(self, gateway):
# if options['gateway']:
# mark_gateways(options['gateway'])
def mark_gateways(self, gateways):
for gateway in gateways:
try: try:
node = self.maybe_node_by_mac((gateway, )) node = self.maybe_node_by_mac((gateway, ))
except:
print("WARNING: did not find gateway '",gateway,"' in node list")
continue
node.flags['gateway'] = True node.flags['gateway'] = True
except KeyError:
print("WARNING: did not find gateway ", gateway, " in node list")
def update_vpn_links(self): def update_vpn_links(self):
changes = 1 changes = 1