#!/usr/bin/lua --[[ bmx2hosts Copyright 2017 Nicolas Pace , Nicolas Echaniz Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 ]]-- json = require('luci.jsonc') fs = require('nixio.fs') local function map(func, tbl) local newtbl = {} for i,v in pairs(tbl) do newtbl[i] = func(v) end return newtbl end function filter(func, tbl) local newtbl= {} for i,v in pairs(tbl) do if func(v) then newtbl[i]=v end end return newtbl end local function extract_ips(HNA6_EXTENSION) -- Workaround to avoid bmx6 special IP if string.find(HNA6_EXTENSION.address,"fd66:66:66:ff00:") then return nil else return HNA6_EXTENSION.address end end local function only_HNA6_EXTENSION(obj) return (obj.HNA6_EXTENSION ~= nil) end function readAll(file) local f = io.open(file, "rb") local content = f:read("*all") f:close() return content end function get_ipv6_ips(host_json) json_data = json.parse(host_json) hna6_extensions = filter(only_HNA6_EXTENSION, json_data.DESC_ADV.extensions) local tupd = {} for k in pairs(hna6_extensions) do table.insert(tupd, k) end -- populate table.sort(tupd) -- sort for k, v in ipairs(tupd) do tupd[k] = hna6_extensions[v] end -- replace return map(extract_ips, tupd[1].HNA6_EXTENSION) end function get_hostname(s) return s:gmatch(".+/([%w_-]+)\.")() end local json_iterator, o_count = fs.glob('/var/run/bmx6/json/descriptions/*') for i=1, o_count do local filename = json_iterator() hostname = get_hostname(filename) local ips = get_ipv6_ips(readAll(filename)) for k, ip in ipairs(ips) do if ip ~= nil then print (ip.." "..hostname..'.mesh') end end end