Skip to content

Commit af58aa7

Browse files
authored
Merge pull request #129 from SmithChart/ffbs-debugbathosts
Add ffbs-debugbathosts
2 parents 9ed9621 + db60e8f commit af58aa7

File tree

4 files changed

+139
-0
lines changed

4 files changed

+139
-0
lines changed

ffbs-debugbathosts/Makefile

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
include $(TOPDIR)/rules.mk
2+
3+
PKG_NAME:=ffbs-debugbathosts
4+
PKG_VERSION:=1
5+
PKG_RELEASE:=1
6+
7+
PKG_MAINTAINER:=Chris Fiege <[email protected]>
8+
PKG_LICENSE:=MIT
9+
10+
include $(TOPDIR)/../package/gluon.mk
11+
12+
define Package/$(PKG_NAME)
13+
TITLE:=Provide /etc/bat-hosts.
14+
endef
15+
16+
define Package/$(PKG_NAME)/description
17+
This package provides an empty /etc/bat-hosts file and the script
18+
update-bat-hosts that populates /etc/bat-hosts with entries from
19+
a given nodes.json.
20+
endef
21+
22+
$(eval $(call BuildPackageGluon,$(PKG_NAME)))
23+

ffbs-debugbathosts/README.md

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
ffbs-debugbathosts
2+
==================
3+
4+
This script provides means to map MAC addresses to names for `batctl`.
5+
6+
This is done with two components:
7+
8+
* `/etc/bat-hosts` is a mapping file consumed by `batctl`.
9+
It is delivered empty and only contains help for the user.
10+
It is mostly intended as a reminder that this functions exists at all.
11+
* `update-bat-hosts` is a lua-script that loads a `nodes.json` from a given URL
12+
and populates `/etc/bat-hosts` with the nodes found.
+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Map mac addresses to names for batctl
2+
# Format:
3+
# <mac> <name>
4+
#
5+
# For your convenience the following script populates this file for you:
6+
# $ update-bat-hosts <url-to-nodes-json> [--gw]
7+
8+
# example:
9+
00:1f:16:12:8f:ae chrissi-korolev-eth
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
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

Comments
 (0)