|
| 1 | +#!/usr/bin/lua |
| 2 | +-- SPDX-FileCopyrightText: Florian Maurer |
| 3 | +-- SPDX-License-Identifier: MIT |
| 4 | + |
| 5 | +local jsonc = require("jsonc") |
| 6 | + |
| 7 | +local function truncate_hostname(hostname, max_length) |
| 8 | + -- If the hostname is already within the maximum length, return it as is |
| 9 | + if #hostname <= max_length then |
| 10 | + return hostname |
| 11 | + end |
| 12 | + |
| 13 | + -- Define the number of characters to keep at the start and end |
| 14 | + local keep_start = math.floor((max_length - 3) / 2) |
| 15 | + local keep_end = max_length - 3 - keep_start |
| 16 | + |
| 17 | + -- Concatenate the start, ellipses, and end of the hostname |
| 18 | + return hostname:sub(1, keep_start) .. "..." .. hostname:sub(-keep_end) |
| 19 | +end |
| 20 | + |
| 21 | +-- Function to fetch JSON data using wget |
| 22 | +local function fetch_json_from_url(url) |
| 23 | + local tmpfile = "/tmp/nodes.json" -- Temporary file to store the downloaded JSON data |
| 24 | + local success = os.execute("wget -q -O " .. tmpfile .. " " .. url) |
| 25 | + if success > 0 then |
| 26 | + error("Failed to download JSON file from " .. url) |
| 27 | + end |
| 28 | + local file = io.open(tmpfile, "r") |
| 29 | + if not file then |
| 30 | + error("Failed to open the downloaded JSON file") |
| 31 | + end |
| 32 | + |
| 33 | + local json_data = file:read("*all") |
| 34 | + file:close() |
| 35 | + |
| 36 | + return json_data |
| 37 | +end |
| 38 | + |
| 39 | +-- Function to parse the JSON data and output MAC addresses with hostnames |
| 40 | +local function parse_and_print_mac_addresses(json_data, gateway_only) |
| 41 | + local data = jsonc.parse(json_data) |
| 42 | + if not data then |
| 43 | + error("Failed to parse JSON data") |
| 44 | + end |
| 45 | + local seen_hostnames = {} -- Table to keep track of seen hostnames |
| 46 | + |
| 47 | + local file = io.open("/tmp/bat-hosts", "w") |
| 48 | + if not file then |
| 49 | + error("Failed to open /tmp/bat-hosts") |
| 50 | + end |
| 51 | + |
| 52 | + file:write("# map mac addresses to names for batctl\n") |
| 53 | + file:write("# format:\n") |
| 54 | + file:write("# <mac> <name>\n") |
| 55 | + file:write("#\n") |
| 56 | + file:write("# For your convenience the following script populates this file for you:\n") |
| 57 | + file:write("# $ update-bat-hosts <url-to-nodes-json> [--gw]\n\n") |
| 58 | + |
| 59 | + -- Iterate over nodes and extract MAC addresses with corresponding hostnames |
| 60 | + for _, node in ipairs(data.nodes) do |
| 61 | + local hostname = truncate_hostname(node.nodeinfo.hostname:gsub("%s", "-"), 30) |
| 62 | + local mesh = node.nodeinfo.network.mesh |
| 63 | + |
| 64 | + -- only parse if gateway filter is off or is gateway |
| 65 | + -- and if mesh info exists and host was not seen yet (otherwise this would give warnings) |
| 66 | + if (not gateway_only or node.flags.gateway) and mesh and not seen_hostnames[hostname] then |
| 67 | + seen_hostnames[hostname] = true |
| 68 | + for _, mesh_interface in pairs(mesh) do |
| 69 | + local interfaces = mesh_interface.interfaces |
| 70 | + -- Loop through wireless, other, and tunnel interfaces |
| 71 | + for iface, mac_list in pairs(interfaces) do |
| 72 | + for idx, mac in ipairs(mac_list) do |
| 73 | + file:write(mac .. " " .. hostname .. "-" .. iface .. "" .. idx .. "\n") |
| 74 | + end |
| 75 | + end |
| 76 | + end |
| 77 | + end |
| 78 | + end |
| 79 | +end |
| 80 | + |
| 81 | +if #arg == 0 then |
| 82 | + print("Useage: update-bat-hosts <nodes.json url> [--gw]") |
| 83 | + os.exit(1) |
| 84 | +end |
| 85 | + |
| 86 | +local url = arg[1] |
| 87 | + |
| 88 | +local gateway_only = false |
| 89 | +if #arg == 2 and arg[2] == '--gw' then |
| 90 | + gateway_only = true |
| 91 | +end |
| 92 | + |
| 93 | +local json_data = fetch_json_from_url(url) |
| 94 | +parse_and_print_mac_addresses(json_data, gateway_only) |
| 95 | +os.execute("mv /tmp/bat-hosts /etc/bat-hosts") |
0 commit comments